HDU2609 How many(字符串的最小表示法)

来源:互联网 发布:java elasticsearch 编辑:程序博客网 时间:2024/05/28 17:08

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

思路

给你n组字符串,每一个字符串可以自由的左移和右移,问一共有多少种字符串,我们可以用字符串的最小表示法把每一个字符串都表示一下,然后插入set中,返回元素数量就是要求的答案

代码

#include<cstdio>#include<cstring>#include<string>#include<set>#include<iostream>#include<stack>#include<queue>#include<vector>#include<algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define ll long longusing namespace std;set<string>st;int max_min_express(string s,bool flag){    int i=0,j=1,k=0,len=s.length();    while(i<len&&j<len&&k<len)    {        int t=s[(j+k)%len]-s[(i+k)%len];        if(t==0)k++;        else        {            if(flag)            {                if(t>0) j+=k+1;                else i+=k+1;            }            else            {                if(t>0) i+=k+1;                else j+=k+1;            }            if(i==j) j++;            k=0;        }    }    return min(i,j);}string change(string s)//组合字符串{    int k=max_min_express(s,true);    int len=s.length();    string str="";    for(int i=k+1; i<=k+len; i++)        str+=s[i%len];    return str;}int main(){    int n;    while(cin>>n)    {        string s;        st.clear();        while(n--)        {            cin>>s;            string str=change(s);            st.insert(str);        }        cout<<st.size()<<endl;    }    return 0;}
原创粉丝点击