How many
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1650 Accepted Submission(s): 664
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.
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').
4 0110 1100 1001 0011 4 1010 0101 1000 0001
12
最小表示模板加set
#include<cstdio> #include<iostream> #include<string> #include<cstring> #include<set> using namespace std; int MinimumRepresentation(char *s, int l) { int i = 0, j = 1, k = 0, t; while(i < l && j < l && k < l) { t = s[(i + k) >= l ? i + k - l : i + k] - s[(j + k) >= l ? j + k - l : j + k]; if(!t) k++; else{ if(t > 0) i = i + k + 1; else j = j + k + 1; if(i == j) ++ j; k = 0; } } return (i < j ? i : j); } set<string> x; int main() { int n=0; while(scanf("%d",&n)!=EOF) { getchar(); char s1[1000]; char s2[1000]; for(int i=1;i<=n;i++) { int kk=0; int num=0; gets(s1); strcpy(s2,s1); strcat(s1,s2); kk=strlen(s1); num=MinimumRepresentation(s1,kk); s1[num+kk/2]='\0'; x.insert(s1+num); } printf("%d\n",x.size()); x.clear(); } return 0; }