D - Periodic Strings(UVA-455)

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



挺简单的一道题,但是不知道为什么做了一天,可能是因为今天不在状态。刚开始大体想了一个思路,但是后来发现那个思路里有很多错误,经过各种修改后就开始有点混乱了,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;
}

 

转载于:https://www.cnblogs.com/Highmath/p/4168471.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值