周期串(Periodic Strings,UVa455)

 

解题思路:

对一个字符串求其最小周期长度,那么,最小周期长度必定是字符串长度的约数,即最小周期长度必定能被字符串长度整除

其次,对于最小周期字符串,每位都能对应其后周期字串的每一位,

即 ABC  ABCABC (345678)->%其字串长度3

    012  3%3 4%3 5%3  6%3 7%3  8%3

          0      1     2        0      1       2

if(a[j]!=a[j%3])说明不对应,不是周期,进行下一位扫描。

AC Code:

#include<stdio.h>
#include<string.h>
int main(void)
{
    int n,stlen,i,j;
    char carr[1000];
    while(scanf("%d",&n)!=EOF)
    {
        while(n--)
        {
            scanf("%s",carr);
            stlen=strlen(carr);
            for(i=1; i<=stlen; i++)
            {
                if(stlen%i==0)
                {
                    for(j=i; j<stlen; ++j)
                        if(carr[j]!=carr[j%i])break;
                    if(j==stlen)
                    {
                        printf("%d\n",i);
                        break;
                    }
                }
            }
            if(n)printf("\n");
        }
    }
    return 0;
}

  

posted @ 2016-07-21 21:24  马丁黄瓜啊  阅读(1717)  评论(0编辑  收藏  举报