标签:style blog http ar io color sp for on
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.
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.
An integer denoting the smallest period of the input string for each input. Two consecutive output are separated by a blank line.
1 HoHoHo
2
挺简单的一道题,但是不知道为什么做了一天,可能是因为今天不在状态。刚开始大体想了一个思路,但是后来发现那个思路里有很多错误,经过各种修改后就开始有点混乱了,WA了几次后经加加提醒发现当输入的字符串是ASDFG这样的时,我的理解有所偏差,答案应该是5而不是0.输出的格式一开始也没看到两个输出之间有一个空行,后来又修改了几次,还是不对,抓来了男神的代码研究了一下,换了一种思路,可是又出现了很多语法错误= =幸好最后A掉了。。贴一下代码。。
#include <stdio.h>
#include <string.h>
int isLoopstring(char str[],int a,int b);
int main()
{
int n,i,k;
char str[85];
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
for(i=1;i<=strlen(str);i++)
{
if(strlen(str)%i==0)
{
if(isLoopstring(str,i,strlen(str)))
{
k=i;
break;
}
}
}
if(n==0)
printf("%d\n",k);
else
printf("%d\n\n",k);
}
return 0;
}
int isLoopstring(char str[],int a,int b)
{
int i,j,flag=1;
for(i=0;i<a;i++)
{
for(j=i+a;j<b;j+=a)
{
if(str[j]!=str[i])
{
flag=0;
return 0;
}
}
}
if(flag) return 1;
}
标签:style blog http ar io color sp for on
原文地址:http://www.cnblogs.com/Highmath/p/4168471.html