您的位置:首页 > 其它

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

1

HoHoHo

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva