uva455周期串问题 求好心人看看哪里错了提交就是wa

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>

char str[150];

int main()
{
int N;
char c;
scanf("%d",&N);
c=getchar();
while(N--)
{
memset(str,0,sizeof(str));
gets(str);
int len=strlen(str);
int i,t,sum,m=0;
for(i=1; i<=strlen(str); i++)
{
if(len%i==0)
{
for(t=i; t<len; t++)
{
if(str[t]==str[t-i])
{
m=1;
}
else
{
m=0;
break;
}
}
if(m==1)
{
sum=i;
printf("%d\n",sum);
if(N)
printf("\n");
break;

}

}
}
if(m==1)
continue;
else
{
printf("%d\n",len);
if(N)
printf("\n");

}

}
return 0;

}``````
...全文
141 4 点赞 打赏 收藏 举报

4 条回复

FancyMouse 2017-11-09
Two consecutive input will separated by a blank line. 这种时候不要用gets这种对空行敏感的输入。谁知道这数据会出成什么样子。
• 打赏
• 举报

• 打赏
• 举报

• 打赏
• 举报

``````#include <stdio.h>
#include <string.h>

#define MAXN 80

int strcmplen(char a[], int s, int t, int len)
{
int i;

for(i=1; i<=len; i++) {
if(a[s] != a[t])
return a[s] - a[t];

s++;
t++;
}

return 0;
}

int main(void)
{
int n, len, i, j;
char s[MAXN+1];

scanf("%d", &n);
while(n--) {
scanf("%s", s);

len = strlen(s);

for(i=1; i<len; i++) {
if(len % i)
continue;

for(j=i; j<len; j+=i)
if(strcmplen(s, 0, j, i) != 0)
break;

if(j == len)
break;
}

printf("%d\n", i);
if(n)
printf("\n");
}

return 0;
}  ``````
http://blog.csdn.net/tigerisland45/article/details/52056047
• 打赏
• 举报

C语言

6.5w+

C语言相关问题讨论

2017-11-08 01:16