HDU 2609 How many (字符串最小字典序+set去重)

题目链接

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

PS:题意大家都应该明白吧。题解:将字符串看成环,如果一个字符串通过旋转得到的另一个字符串,这两个字符串的最小字典序一定是一样的。所以我们直接将每个字符串的最小字典序压入set中,如果有题中所说的一类的字符串,那么压入的最小字典序的字符串一定一样,而set可以去重,所以最后set的大小,就是n个字符串可以分成多少类,详解看代码。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e2+10;
const int mod=10007;
const int inf=1e8;
#define me(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&(-x)
typedef long long ll;
using namespace std;
char str[maxn];
int len;
set<string>q;
int min_pos()//找到字符串最小字典序的相应位置
{
    int p1=0,p2=1,k=0;
    while(p1<len&&p2<len&&k<len)
    {
        int temp=str[(p1+k)%len]-str[(p2+k)%len];
        if(!temp)
            k++;
        else
        {
            temp>0?p1+=k+1:p2+=k+1;
            if(p1==p2)
                p2++;
            k=0;
        }
    }
    return min(p1,p2);
}
void Insert()//将每个字符串以最小字典序插入set中
{
    char temp[maxn];
    int pos=min_pos();//找字符串的最小字典序位置
    int l=0;
    for(int i=pos; i<len; i++)//将字符串转换成最小字典序形式
        temp[l++]=str[i];
    for(int i=0; i<pos; i++)//将字符串转换成最小字典序形式
        temp[l++]=str[i];
    temp[len]='\0';
    q.insert(temp);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        q.clear();
        for(int i=0; i<n; i++)
        {
            scanf("%s",str);
            len=strlen(str);
            Insert();
        }
        cout<<q.size()<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41292370/article/details/85106195