[HDU2609]How many

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Give you n ( n &lt; 10000 ) n ( n &lt; 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 − &gt; 1100 − &gt; 1001 − &gt; 0011 − &gt; 0110 0110 -&gt; 1100 -&gt; 1001 -&gt; 0011-&gt;0110 0110>1100>1001>0011>0110.

Input
The input contains multiple test cases.
Each test case include: first one integers n . ( 2 &lt; = n &lt; = 10000 ) n. (2&lt;=n&lt;=10000) n.(2<=n<=10000)
Next n lines follow. Each line has a equal length character string. (string only include ′ 0 ′ , ′ 1 ′ &#x27;0&#x27;,&#x27;1&#x27; 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 − &gt; 1100 − &gt; 1001 − &gt; 0011 − &gt; 0110 0110 -&gt; 1100 -&gt; 1001 -&gt; 0011-&gt;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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值