hdu2609 How many (最小表示法)

http://acm.hdu.edu.cn/showproblem.php?pid=2609

题意:

给出n个字符串,如果字符串A循环同构可变成B,则A与B是相同的,问有多少个不同的字符串。

用最小表示法表示出每个字符串,然后map判重即可

最小表示法转向https://www.cnblogs.com/TheRoadToTheGold/p/7040955.html

#include<map>
#include<cstdio>
#include<cstring> 
#include<iostream>
#include<algorithm>

using namespace std;

#define N 10001

string a[N];
char s[1000001];
int len;
map<string,int>mp;

int getmin()
{
    len=strlen(s); 
    int i=0,j=1,k;
    while(i<len && j<len)
    {
        k=0;
        while(k<len && s[(i+k)%len]==s[(j+k)%len]) k++;
        if(k==len) break;
        if(s[(i+k)%len]<s[(j+k)%len]) j=max(i+1,j+k+1);
        else i=max(j+1,i+k+1);
    }
    return min(i,j);
}

int main()
{
    int n,ans;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;++i) a[i].clear();
        mp.clear();
        ans=0;
        for(int i=1;i<=n;++i) 
        {
            scanf("%s",s);
            int st=getmin();
            for(int j=st;j<len;++j) a[i]+=s[j];
            for(int j=0;j<st;++j) a[i]+=s[j];
            mp[a[i]]++;
            if(mp[a[i]]==1) ans++;
        }
        printf("%d\n",ans); 
    }
}

猜你喜欢

转载自www.cnblogs.com/TheRoadToTheGold/p/12913414.html