hdu2609最小表示法加去重

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

判断同构数目,把所有最小表示法求出来,用set去重即可 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#define maxn 10005
using namespace std;
int len;
int t;
int m;
set<string>ss;
char s[maxn];
char p[maxn];
int getmin(char*s)
{
    int n=strlen(s);
    int i =0,j=1,k=0;
    while(i<n&&j<n&&k<n)
    {
        int t=s[(i+k)%n]-s[(j+k)%n];
        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);
}
void slove(char*str)
{
    str[len/2]='\0';
    ss.insert(str);
}
int main()
{
    while(~scanf("%d",&m))
    {ss.clear();
        for(int i=1;i<=m;i++)
        {scanf("%s",p);
        strcpy(s,p);
        strcat(s,p);
        len=strlen(s);
        int d=getmin(p);
        slove(s+d);

    }
    printf("%d\n",ss.size());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/87989723
今日推荐