码迷,mamicode.com
首页 > 其他好文 > 详细

周期串

时间:2014-09-15 19:36:39      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:style   ar   for   sp   on   c   line   size   r   

    Problem:                   

                                              如果一个字符串可以由某个长度为k的字符串重复多次得到,则该串以k为周期。例如,abcabcabcabc

                                              3为周期(注意,它也以6和12为周期)。输入一个长度不超过80的串,输出它的最小周期。

    Program:             

                       

#include<cstring>
#include<string>
using namespace std;
const int maxn=10009;

int main()
{
    char word[maxn];
    while(scanf("%s",word)!=EOF)
    {
        int str=strlen(word);
        for(int i=1; i<str; i++)
            if(str%i==0)
        {    
            int ok=1;
            for(int j = i;j< str;j++)

                if(word[j]!=word[j%i])
                {
                    ok=0;break;
                }
                if(ok)   {printf("%d\n",i);break;}


        }
    }
    return 0;

}

     

                      

周期串

标签:style   ar   for   sp   on   c   line   size   r   

原文地址:http://blog.csdn.net/u013514722/article/details/39295727

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!