How many
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2165 Accepted Submission(s): 893
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
题目大意:给你n 个字符串,这些字符串可以通过循环移位,求通过变换之后,有多少种不同的字符串
思路:用统一的方式来表示,可以用最小表示法,模板题,比较简单
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#define LL long long
using namespace std;
const int maxn = 10000+5;
string s[maxn];
int getMin(string ss)
{
int ls=ss.size();
int i=0,j=1,k=0;
while(i<ls && j<ls && k<ls){
if(ss[(i+k)%ls]==ss[(j+k)%ls]) k++;
else{
if(ss[(i+k)%ls]>ss[(j+k)%ls]) i=i+k+1;
else j=j+k+1;
if(i==j) j++;
k=0;
}
}
return min(i,j);
}
int main()
{
int n,pos,ans;
string ss;
while(cin>>n){
for(int i=0;i<n;i++){
cin>>ss;
pos=getMin(ss);
//cout<<"pos="<<pos<<endl;
s[i]=ss.substr(pos)+ss.substr(0,pos);
//cout<<"s[i]="<<s[i]<<endl;
}
sort(s,s+n);
ss=s[0];
ans=1;
for(int i=1;i<n;i++){
if(ss==s[i]) continue;
ss=s[i];
ans++;
}
cout<<ans<<endl;
}
return 0;
}