算法竞赛入门经典(第二版) 习题3-4 周期串(Periodic Strigs) UVa455
2015-08-07 19:59
148 查看
Page 57
题意:输入一个长度不超过80的字符串,输出其最小正周期。
思路:周期一定能被字符串长度整除,故枚举所有可能的周期,逐个判断直到找到最小正周期。
题意:输入一个长度不超过80的字符串,输出其最小正周期。
思路:周期一定能被字符串长度整除,故枚举所有可能的周期,逐个判断直到找到最小正周期。
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cctype> #include<cmath> using namespace std; #define max 85 char s[max]; int main(){ //freopen("in.txt","r",stdin); int T,i,j,l; scanf("%d",&T); while(T--){ scanf("%s",s); l=strlen(s); for(i=1;i<=l/2+1;i++){ if(l%i==0){ int n=1,j=i; while(s[j]!=0){ if(s[j]!=s[j%i]) { n=0;break;} j++; } if(n){ if(T==0) printf("%d\n",i); else printf("%d\n\n",i); break; } } else if(i==l/2+1){ if(T==0) printf("%d\n",l); else printf("%d\n\n",l); } } } return 0; }
相关文章推荐
- hdu 5297 容斥原理
- 修改Qweb报表
- 本地安装UFT插件到Chrome
- java注意事项
- latex笔记
- 通讯录实战
- POJ 2109 Power of Cryptography
- 数值的交换
- MVVM(Model-View-ViewModel)框架——avalon。
- MySQL性能调优与架构设计——第3章 MySQL存储引擎简介
- hdoj 4552 怪盗基德的挑战书【求前缀在字符串中出现的次数之和】
- [LeetCode]Reverse Linked List
- 应用程序创建DMG打包发布
- LeetCode #214 Shortest Palindrome
- POJ 2013 Symmetric Order 水
- POJ-1922 Ride to School
- Android Java设置一个图片的颜色为透明色(不显示,不是背景色),可用于无透明通道的ImageView
- ZOJ3818 Pretty Poem(暴力)
- swirl 7: Matrices and Data Frames
- VC程序版本更新后部分客户无法启动