UVa 455 - Periodic Strings
2016-07-19 10:54
381 查看
Periodic Strings
A character string is said to have period k if it can be formed by concatenating one or more repetitions of another string of length k. For example, the string “abcabcabcabc” has period 3, since it is formed by 4 repetitions of the string “abc”. It also has periods 6 (two repetitions of “abcabc”) and 12 (one repetition of “abcabcabcabc”).Write a program to read a character string and determine its smallest period.
Input
The first line oif the input file will contain a single integer N indicating how many test case that your program will test followed by a blank line. Each test case will contain a single character string of up to 80 non-blank characters. Two consecutive input will separated by a blank line.Output
An integer denoting the smallest period of the input string for each input. Two consecutive output are separated by a blank line.Sample Input
1HoHoHo
Sample Output
2现在做题都是先看输入输出结果,此题一看就是水题啊,但是我卡住了。。。
为什么卡住,有待反思
AC
#include <stdio.h> #include <string.h> char str[81]; int main() { int n; scanf("%d",&n); while(n--) { scanf("%s",str); getchar(); int len=strlen(str); int t,i; for(t=1; t<=len; t++)//t为周期 { if(len%t==0) { for(i=t; i<len; i++)//写成i=0,也是可以的,但是i=t更优 if(str[i%t]!=str[i]) break;//如果不满足循环条件就跳出循环 } if(i==len)//如果上述循环顺利完成,i=n { printf("%d\n",t); if(n!=0)printf("\n");//此题的一个坑就是要控制输出格式,看书上是完全看不出来要输出控制的 break; } } } return 0; }
break 和 continue的区别
break是结束整个循环体,continue是结束单次循环
ACM训练——刘汝佳《算法竞赛入门经典》
传送门——UVa
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?