How many

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

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
 
Author
yifenfei
 
Source
 
Recommend
yifenfei
 

题意:

给定n个01串,统计一共有多少种不同的。如果他们的循环同构是相同的那他们就是相同的。

思路:

刚开始很暴力的把每个串的所有循环同构串都丢进set里,直接MLE了。太暴力了。

应该要想到如果串相同的话,他们的最小表示法肯定是一样的。所以只需要存最小表示法就可以了。

然后也不需要统计,直接print set的大小就可以了。

每输入一个数,找到他的最小表示法。

然后对这个最小表示法进行hash,结果丢进set

处理完所有字符串,print set的大小。

 #include<iostream>
#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
//#include<cstdlib>
#include<cstring>
#include<algorithm>
//#include<queue>
#include<vector>
//#include<set>
//#include<climits>
//#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define N 100010
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = 1e4 + ;
int n;
char str[ * ];
ull h[];
set<ull>types; int main()
{
while(scanf("%d", &n) != EOF){
types.clear();
for(int i = ; i < n; i++){
scanf("%s", str + );
int len = strlen(str + );
for(int j = ; j <= len; j++)str[len + j] = str[j];
int j = , k = , p;
while(j <= len && k <= len){
for(p = ; p < n && str[j + p] == str[k + p];p++);
if(p == len)break;
if(str[j + p] > str[k + p]){
j = j + p + ;
if(j == k)j++;
}
else{
k = k + p + ;
if(j == k)k++;
}
}
int pos = min(j, k); for(j = ; j <= len; j++){
h[j] = h[j - ] * + str[j + pos - ] - '';
}
types.insert(h[len]);
}
printf("%d\n", types.size());
}
return ;
}
05-11 14:06