hdu 2609 How many 最小表示法

How many

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1248    Accepted Submission(s): 486


Problem Description
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me
How many kinds of necklaces total have.(if two necklaces can equal by rotating ,we say the two necklaces are some).
For example 0110 express a necklace, you can rotate it. 0110 -> 1100 -> 1001 -> 0011->0110.
 
Input
The input contains multiple test cases.
Each test case include: first one integers n. (2<=n<=10000)
Next n lines follow. Each line has a equal length character string. (string only include '0','1').
 
Output
For each test case output a integer , how many different necklaces.
 
Sample Input
4
0110
1100
1001
0011
4
1010
0101
1000
0001
 
Sample Output
1
2
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <string.h>
 5 #include <set>
 6 using namespace std;
 7 set<string>ss;
 8 void make(char a[],int x,int l)
 9 {
10     char b[150];
11     int i;
12     for(i=0; i<l; i++)
13         b[i]=a[x+i>=l?x+i-l:x+i];
14     b[i]='';
15     ss.insert(b);
16 }
17 void moremin(char a[])
18 {
19     int len=strlen(a);
20     int i,j,k,t;
21     k=i=0;
22     j=1;
23     while(i<len&&j<len&&k<len)
24     {
25         int t=a[i+k>=len?i+k-len:i+k]-a[j+k>=len?j+k-len:j+k];
26         if(!t)k++;
27         else
28         {
29             if(t>0) i+=k+1;
30             else j+=k+1;
31             if(i==j)j++;
32             k=0;
33         }
34     }
35     make(a,(i>j?j:i),len);
36 }
37 int main()
38 {
39     int n;
40     char a[150];
41     while(~scanf("%d",&n))
42     {
43         ss.clear();
44         while(n--)
45         {
46             scanf("%s",a);
47             moremin(a);
48         }
49         cout<<ss.size()<<endl;
50     }
51 }
View Code
 
原文地址:https://www.cnblogs.com/ERKE/p/3832893.html