/* 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
*/
#include<stdio.h>
#include<string.h>
int main()
{
int n;
char a[100];
int i,j,k,p,q;
scanf("%d",&n);
while(n--)
{
int m=1;
memset(a,0,sizeof(a));
scanf("%s",a);
for(i=1;i<strlen(a);i++)
{
p=1;
q=0;
if(a[0]==a[i]&&strlen(a)%i==0)
{
for(j=0;j<i&&p;j++)
for(k=1;k*i+j<strlen(a)&&p;k++)
if(a[j]!=a[j+k*i]) {p=0;}
else p=1;
q=1;
}
if(p&&q) {printf("%d\n",i);m=0;break;}
}
if(m) printf("%d\n",strlen(a));
if(n!=0) printf("\n");
}
//printf("~");
return 0;
}
//莫名奇妙的wa,不知道为什么,过了我自己的很多样例。
/*已AC,因为原题说 Output
An integer denoting the smallest period of the input string for each input. Two consecutive output are separated by a blank line.
输出数据之间用用一个空行分开。
我没分开,它说是 WA而不是PE所以还是如入门经典说的好,一些格式错误它把规到WA一类了。
这题要注意以下易错的地方:
当aaaaaaaaaa为数据,周期是1;
当一个数据没周期eg:aaaabbbb,周期输出为它字符串的长度,(由题意得出来的)
和空格问题一样,最后一行数据就不需再输出一个空行了(即输出了两个\n),直接输出一个\n就好,试过了,否则又算WA。
*/
uva455 Periodic Strings
最新推荐文章于 2024-10-23 00:33:47 发布