hdu 2609 How many

来源:互联网 发布:奥尼尔新秀赛季数据 编辑:程序博客网 时间:2024/06/07 03:26

点击打开链接

How many

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3251    Accepted Submission(s): 1446


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
4011011001001001141010010110000001
 

Sample Output
12
 
题不是很难,怪我没想到。借口,借口,还是能力不够。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <set>using namespace std;char str[10005][105];char st[10005][105];int mi_ma(char *s,int len){    int i = 0, j = 1, k = 0;    while(i < len && j < len && k < len)    {        int t = s[(j + k) % len] - s[(i + 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);}int main(){    int n;    while(cin >> n)    {        for(int i = 0; i < n; i++)        {            scanf("%s", str[i]);            int len=strlen(str[i]);            int t = mi_ma(str[i],len);            int j=0,k;            for(k=t;k<len;k++)            st[i][j++]=str[i][k];            for(k=0;k<t;k++)            st[i][j++]=str[i][k];            st[i][j]='\0';        }        set<string>ff;        for(int i=0;i<n;i++)        ff.insert(st[i]);        cout<<ff.size()<<endl;    }    return 0;}

参考博客

http://blog.csdn.net/piaocoder/article/details/48447193



原创粉丝点击