明知道这是一场意外 2016-03-23 05:03 采纳率: 66.7%
浏览 1470
已采纳

刷ACM的小伙伴进uva oj 455 Periodic Strings,求大神指出我的问题

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

简单说就是输入一个长度不超过80个字符串,输出其最小周期(注意题目有关空行的格式问题) 我WA了五次,渣渣被虐哭囧

下面的是我的代码,求给出反例或指出我的逻辑错误,谢谢

 //要多注意细节,多考虑极端情况(注意循环字符中还有相同字符的情况!!!)
#include <stdio.h>
#include <string.h>
int main()
{
    int T,len,i,k,temp,first=1;//T代表测试块的个数,len是字符串长度,temp存储可能的周期值
    char str[90]={0};//存储字符串
    scanf("%d",&T);
    while(T--){
        temp=0,k=0;
        scanf("%s",str);
        len=strlen(str);
        for(i=1;i<len;i++){
            if(str[i]==str[0]){
                temp=i;
                for(k=0;k<temp;k++,i++){
                    if(str[k]!=str[i]){//反向思维,针对ABABACC的情况
                        temp=0;
                        i--;
                        break;
                    }
                    if(k!=temp-1&&i==len-1){//主要针对ABCDAB的情况
                        temp=0;
                        break;
                    }
                    if((k==temp-1)&&(i!=len-1))
                        k=-1;//始终注意k的自加
                }
            }
        }
        if(i==len&&!k)//注意不存在周期的情况!
            temp=0;
        if(first){
            printf("%d\n",temp);
            first=0;
        }
        else
            printf("\n%d\n",temp);
    }
    return 0;
}

  • 写回答

1条回答 默认 最新

  • 林深 2016-03-23 05:50
    关注

    反例:
    输入: abc
    正确输出:3
    错误输出:0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog