hdu2609最小表示法

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn=10005;
struct elem
{
    char str[105];
    int len;
    bool operator <(const elem &rhs)const{
       return len<rhs.len||(len==rhs.len&&strcmp(str,rhs.str)<=0);
    }
    bool operator == (const elem &rhs)const{
       return len==rhs.len&&strcmp(str,rhs.str)==0;
    }
}P[maxn];
char s[105];
int MinRepresstation(char * S, int len ) {
      int i = 0, j = 1, k = 0;
      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])
            i = max(i + k + 1, j + 1);
           else
            j = max(i + 1, j + k + 1);
        }
      return min(i ,j);
}
int main()
{
    int n;
    while(scanf("%d",&n)==1)
        {
             for(int i=0; i<n; i++)
                {
                      scanf("%s",s);
                      P[i].len=strlen(s);
                      int d=MinRepresstation(s,P[i].len);
                      for(int j=0;j<P[i].len; j++)
                        P[i].str[j]=s[ (d+j)%P[i].len ];
                      P[i].str[P[i].len]=0;
                }
             sort(P,P+n);
             int num=unique(P,P+n)-P;
             printf("%d\n",num);

        }

    return 0;
}

 

posted @ 2015-09-15 15:35  来自大山深处的菜鸟  阅读(168)  评论(0编辑  收藏  举报