hdu-2609-How many(串的最小表示)

题目链接

 1 /*
 2     Name:hdu-2609-How many
 3     Copyright:
 4     Author:
 5     Date: 2018/4/24 15:47:49
 6     Description:
 7     串的最小表示 
 8     求出每个串的最小表示,用set去重 
 9 */
10 #include <iostream>
11 #include <cstdio>
12 #include <algorithm>
13 #include <set>
14 
15 using namespace std;
16 string smallestRepresation (string s)  {
17     int i, j ,k ,l;
18     int N = s.length();
19     s += s;
20     for (i=0, j=1; j<N;) {
21         for (k=0; k<N && s[i+k] == s[j+k]; k++) ;
22         if (k >=N ){
23             break;
24         }
25         if (s[i+k] <s[j+k]) {
26             j += k+1;
27         } else {
28             l = i + k;
29             i = j;
30             j = max(l ,j) + 1;
31         }
32     }
33     return s.substr(i, N);
34 }
35 int main()
36 {
37     int n;
38     set<string> capt;
39     while (cin>>n) { 
40         string str;
41         capt.clear();
42         for (int i=0; i<n; i++) {
43             cin>>str;
44             capt.insert(smallestRepresation(str));
45         }
46         cout<<capt.size()<<endl;
47     }
48     return 0;
49 }
原文地址:https://www.cnblogs.com/langyao/p/8931299.html