Luo Gu P1808 _NOI word Classification Guide 2011 increase (01) string sorting

Luo Gu P1808 _NOI word Classification Guide 2011 increase (01)

Title Description

Oliver decided to learn English words back pain, but soon he found disorganized word is very difficult to directly Remember, he decided to classify words.

Two words can be divided into a class if and only if the number of words each consisting of two letters are the same.

For example, "AABAC", and it "CBAAA" can be classified as a class, and while "AAABB" is not a class.

Oliver now there are N words, all words by capital letters, each word length does not exceed 100. Oliver to tell you these words are divided into several categories.

Input Format

The first line of the input file number of words N, the N word lines of each behavior.

Output Format

Output file contains only a number, which represents the number of N words into categories

Sample input and output

Input # 1

AABAC  
CBAAA  
AAABB
Output # 1
2

Description / Tips

For 70% of the data satisfies N≤100. To 100% of the data satisfies N≤10000.

Solution

Algorithm(1)

Each sorted string itself an internal

Then sort all strings

Initialization ans = 1; because there will be at least one type of

Enumerate all the strings, if str [i]! = Str [i + 1], ans ++;

Finally, the output ans

The largest number of operations:

100*log(100)*10000+10000*log(10000)+10000

Approximately equal to O (nlog (n)), right?

Code (100 minutes)

Facts have proved that this is able to live in

It does not require more complex algorithms behind

. 1 #include <the iostream> // do not want to get the job OI, do not use universal head 
2 #include <algorithm> // quick drain Sort () 
. 3 #include <CString>
 . 4  the using  namespace STD;
 . 5  BOOL cmpchar ( char TA, char TB) {
 . 6      return TA < TB;
 . 7  }
 . 8  BOOL cmpstr ( String TA, String TB) {
 . 9      return TA < TB;
 10  }
 . 11  String Word [ 10001 ];
 12 is int n;
13 int main()
14 {
15     cin>>n;
16     int ans=1;
17     for(int i=0;i<n;i++){
18         cin>>word[i];
19         sort(&word[i][0],&word[i][word[i].size()],cmpchar);
20     }
21     sort(&word[0],&word[n],cmpstr);
22     for(int i=0;i<n-1;i++)
23     if(word[i]!=word[i+1])
24         ans++;
25     cout<<ans;
26     return 0;
27 }

The idea is this: find a classification interval, the number of class intervals = +1

ALgorithm(2)

Borrowing STL <map>

map, it can be treated as a custom array

map<string,bool>word

It is to create a new array

Called word

The following table is a string type

Bool value is of type

And it can be as ordinary as direct access to an array

word[str]==1;

Code (100 minutes)

. 1 #include <the iostream> // do not want to get the job OI, do not use universal head 
2 #include <algorithm> // quick drain Sort () 
. 3 #include <CString>
 . 4 #include <cstdio>
 . 5 #include <Map> 
 . 6  the using  namespace STD;
 . 7 Map < String , BOOL > Word;
 . 8  int n-, ANS;
 . 9  String T;
 10  int main ()
 . 11  {
 12 is      Scanf ( " % D " , & n-);
 13 is      for (int i=0;i<n;i++)
14     {
15         cin>>t;
16         sort(t.begin(),t.end());
17         if(!word[t])
18         {
19             ans++;
20             word[t]=1;
21         }
22     }
23     cout<<ans;
24     return 0;
25 }

Ha ha ha, sort could stay like this with yo!

str.begin () str.end () will return str end to end address

This ratio

sort(&word[i][0],&word[i][word[i].size()],cmpchar);

More easy to use, right?

STL Dafa is good!

Guess you like

Origin www.cnblogs.com/send-off-a-friend/p/11334325.html