How many(HDU-2609)

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

题意:每组数据给出 n 个可循环字符串,问有多少种不同的字符串

思路:用最小表示法或最大表示法将所有字符串同化,然后放入 set 容器判重

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define LL long long
const int MOD=10007;
const int N=200000+5;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;
int minmumRepresentation(char *str){//最小表示法
    int len=strlen(str);
    int i=0;//指向字符串最小的位置
    int j=1;//比较指针
    int k=0;//str[i]与str[j]相等时一次移动几个
    while(i<len&&j<len&&k<len){
        int temp=str[(i+k)%len]-str[(j+k)%len];//比较值的长度
        if(temp==0)
            k++;
        else{
            if(temp>0)//维护i
                i=i+k+1;
            else//维护j
                j=j+k+1;
            if(i==j)//相等时比较指针后移
                j++;
            k=0;
        }
    }
    return i<j?i:j;//返回i、j中较小的那个
}
char str[N],temp[N];
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        set<string> S;
        for(int i=0;i<n;i++){
            scanf("%s",str);
            int index=minmumRepresentation(str);//最小表示法的索引
            int len=strlen(str);
            for(int j=0;j<len;j++)
                temp[j]=str[(index+j)%len];
            temp[len]='\0';
            S.insert(temp);
        }
        printf("%d\n",S.size());
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值