Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description
Give you
n
(
n
<
10000
)
n ( n < 10000)
n(n<10000) necklaces ,the length of necklace will not large than
100
100
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
0110
0110 express a necklace, you can rotate it.
0110
−
>
1100
−
>
1001
−
>
0011
−
>
0110
0110 -> 1100 -> 1001 -> 0011->0110
0110−>1100−>1001−>0011−>0110.
Input
The input contains multiple test cases.
Each test case include: first one integers
n
.
(
2
<
=
n
<
=
10000
)
n. (2<=n<=10000)
n.(2<=n<=10000)
Next n lines follow. Each line has a equal length character string. (string only include
′
0
′
,
′
1
′
'0','1'
′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
题意:
给
n
n
n个首位相连的环形的0/1字符串
例子:
0110
−
>
1100
−
>
1001
−
>
0011
−
>
0110
0110 -> 1100 -> 1001 -> 0011->0110
0110−>1100−>1001−>0011−>0110这些字符串算同一种,因为他们相互可以通过循环移位得到.
问有多少种不同的字符串
题解:
使用最小表示法求每个字符串从哪个位置开始循环字典序最小,然后只保留字典序最小的哪个字符串,最后去重统计答案即可。
#include<bits/stdc++.h>
#define LiangJiaJun main
using namespace std;
int n;
int getMin(char *s,int l){
int i=0,j=1,k=0;
while(i<n&&j<n&&k<n){
int t=s[(i+k)%l]-s[(j+k)%l];
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);
}
char s[104];
struct Ds{
char s[104];
int l;
}p[10004];
inline bool dex(Ds A,Ds B){
int l=min(A.l,B.l);
for(int i=0;i<l;i++){
if(A.s[i]!=B.s[i])return A.s[i]<B.s[i];
}
return A.l<B.l;
}
int w33ha(){
for(int i=1;i<=n;i++){
scanf("%s",s);
int l=strlen(s);
int bg=getMin(s,l);
p[i].l=l;
for(int j=0;j<l;j++)p[i].s[j]=s[(bg+j)%l];
}
sort(p+1,p+n+1,dex);
int ans=n;
for(int i=1;i<n;i++){
bool flag=0;
for(int j=0;j<p[i].l;j++){
if(p[i].s[j]!=p[i+1].s[j]){
flag=1;
break;
}
}
if(!flag)--ans;
}
printf("%d\n",ans);
return 0;
}
int LiangJiaJun(){
while(scanf("%d",&n)!=EOF)w33ha();
return 0;
}