How many
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2696 Accepted Submission(s): 1162
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
1 2
#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);
}
}
1252



被折叠的 条评论
为什么被折叠?



