码迷,mamicode.com
首页 > 编程语言 > 详细

算法竞赛入门经典题解——第三章 3-4 周期串UVa455

时间:2018-07-23 14:57:58      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:style   ring   经典   while   str   int   竞赛   char   string   

 

思路:遍历可能的周期,比较s[k]与s[k%i](其中i为周期)

#include <stdio.h>
#include <stdlib.h>
#include<string.h>


int main()
{
    int T;
    char s[90];
    scanf("%d",&T);
    while(T--){
        scanf("%s",s);
        int len,i;
        len=strlen(s);
        for(i=1;i<=len;i++){
            if(len%i==0)
            {
                int k;
                int test=1;
                for(k=0;k<len;k++){
                    if(s[k]!=s[k%i])
                    {
                        test=0;
                        break;
                    }
                }
                if(test){
                    printf("%d\n",i);
                    break;
                }
            }
        }
        if(T) printf("\n");
    }
    return 0;
}

 

算法竞赛入门经典题解——第三章 3-4 周期串UVa455

标签:style   ring   经典   while   str   int   竞赛   char   string   

原文地址:https://www.cnblogs.com/loganlzj/p/9354010.html

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