https://cn.vjudge.net/problem/HDU-2609
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
题意:串是循环的,求不同串个数。
思路:用最小表示法处理每一个串,然后用set容器去重
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+5;
char s[maxn],ss[maxn];
int Next[maxn];
set<string> se;
void get_Next(char s[],int len) {
int j=-1;
Next[0]=-1;
for(int i=1; i<len; i++) {
while(j!=-1&&s[i]!=s[j+1])
j=Next[j];
if(s[i]==s[j+1])
j++;
Next[i]=j;
}
}
int get_repetend_len(int len){//循环节长度
return len-Next[len-1]-1;
}
int get_min(char s[]){//最小表示法
int len=strlen(s);
int i=0,j=1,k=0;
while(i<len&&j<len&&k<len){
int t=s[(i+k)%len]-s[(j+k)%len];
if(t==0) k++;
else{
if(t>0) i+=k+1;
else j+=k+1;
if(i==j) j++;
k=0;
}
}
return min(i,j);
}
int get_max(char s[]){//最大表示法
int len=strlen(s);
int i=0,j=1,k=0;
while(i<len&&j<len&&k<len){
int t=s[(i+k)%len]-s[(j+k)%len];
if(t==0) k++;
else{
if(t>0) j+=k+1;
else i+=k+1;
if(i==j) j++;
k=0;
}
}
return min(i,j);
}
void solve(char s[]){//最小表示法处理环串
int t=get_min(s);
int len=strlen(s);
for(int i=0;i<len;i++){
ss[i]=s[(t+i)%len];
}
ss[len]='\0';
se.insert(ss);
}
int main() {
int n;
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++){
scanf("%s",s);
solve(s);
}
printf("%d\n",se.size());
se.clear();
}
return 0;
}
/*
//KMP算法 判断pattern是否是text的子串
bool KMP(char text[],char pattern[]) {
int n = strlen(text); m = strlen(pattern);
getNext(pattern,m); //计算pattern的Next数组
int j = -1;
for(int i = 0; i < n; i++){
while(j != -1 && text[i] != pattern[j+1]){
j = Next[j]; //j回退使得满足条件或回到原点
}
if(text[i] == pattern[j+1]){
j++; //匹配成功,j指向已匹配的最后一位
}
if(j == m-1){
return true; //已全部匹配完成
}
}
return false; //匹配结束,失败
}
//KMP算法 统计pattern在text出现的次数 文本串“abababab”中模式串“abab”出现了3次
int KMP(char text[],char pattern[]) {
int n = strlen(text); m = strlen(pattern);
getNext(pattern,m); //计算pattern的Next数组
int j = -1,ans = 0;
for(int i = 0; i < n; i++){
while(j != -1 && text[i] != pattern[j+1]){
j = Next[j]; //j回退使得满足条件或回到原点
}
if(text[i] == pattern[j+1]){
j++; //匹配成功,j指向已匹配的最后一位
}
if(j == m-1){
ans++;
j = Next[j];
}
}
return ans;
}
*/