UVa 455 Periodic Strings

本文介绍了一种通过枚举从1到字符串长度的周期来找出字符串最小周期的方法。

题意:给出一个字符串,找出它的最小的周期,枚举从1到len的周期,看是否满足。

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring>  
 4 #include<algorithm>  
 5 using namespace std;
 6 
 7 char s[105];
 8 
 9 int main()
10 {
11     int ncase,t,i,k,j,len;
12     scanf("%d",&ncase);
13     while(ncase--)
14     {
15         scanf("%s",s);len=strlen(s);
16         
17         for(t=1;t<len;t++)//枚举周期从1到len1来找最小的循环节 
18         {
19             if(len%t) continue;//如果不能整除的话就不是周期 
20             int flag=1;
21             for(j=0,k=0;j<len;k=k%t)
22             if(s[j++]!=s[k++]) flag=0;
23             
24             if(flag) break;
25         }
26         
27         printf("%d\n",t);
28         if(ncase!=0)    printf("\n");
29     }
30 }
View Code

 

转载于:https://www.cnblogs.com/wuyuewoniu/p/4307200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值