How many
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2696 Accepted Submission(s): 1162
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.
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').
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
Author
yifenfei
Source
Recommend
yifenfei
题意:问有多少个不同构的字符串 (同构字符串:多次将字符串的第一个元素移到最后所得的多个字符串称同构字符串 Eg:"abcd" , "bcda" , "cdab" , "dabc" 为同构字符串,而最小表示法表示的字符串则为上述字符串中字典序排序最小的一个,即 "abcd" 。
#include<bits/stdc++.h>
using namespace std;
const int N = 10000 + 10;
char a[N][101], b[N][202], c[N][101];
int n;
struct A{
char str[101];
};
int getminsub(char *a)
{
int i = 0,j = 1,len = strlen(a),k = 0;
while(i < len && j < len && k < len)
{
if(k == len) break;
if(i == j) j++;
int ni = i + k, nj = j + k;
if(ni >= len) ni -= len;
if(nj >= len) nj -= len;
if(a[ni] > a[nj])
{
i += k + 1;
k = 0;
}
else if(a[ni] < a[nj])
{
j += k + 1;
k = 0;
}
else k++;
}
return i;
}
int cmp(const void *a,const void *b)
{
A *c, *d;
c = (A *)a;
d = (A *)b;
return strcmp(c -> str, d -> str);
}
int main()
{
while(scanf("%d", &n) == 1)
{
int l;
for(int i = 0; i < n; i++)
{
scanf("%s", a[i]);
if(!i) l = strlen(a[i]);
sprintf(b[i], "%s%s", a[i], a[i]);
int x = getminsub(a[i]);
b[i][x + l] = '\0';
sprintf(c[i], "%s", b[i] + x);
}
qsort(c, n, sizeof(c[0]), cmp); ///这里将字符串按字典序排序
int sum = 1;
for(int i = 1; i < n; i++)
{
if(strcmp(c[i], c[i - 1]))
sum++;
}
printf("%d\n", sum);
}
}