• UVa 455


    UVa OJ 455 

    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

    寻找周期字符串的最小周期。从小到大枚举各个周期,一旦符合条件就输出。注意每一个测试用例的输出结果之间空一行,最后一个用例的输出后面无空行。

    此题个人认为最关键在于对题意的把握,即例如ABCDEF这样的串最后的结果是6而不是0,把握了这一点,多考虑一下即可AC

    /**
     * UVa 455 Periodic Strings
     * Author: Sleigh
     * Last Modified: 2016.03.23
     */
    #include <stdio.h>
    #include <string.h>
    int main()
    {
        int T,len,i,temp,first=1;//T代表测试块的个数,len是字符串长度,temp存储可能的周期值
        char str[90]={0};//存储字符串
        scanf("%d",&T);
        while(T--){
            scanf("%s",str);
            temp=len=strlen(str);//针对ABC这样的情况
            for(i=1;i<len;i++){
                if(str[i]==str[0]){
                    temp=i;
                    for(int k=0;k<temp;k++,i++){
                        if(str[k]!=str[i]){//反向思维,针对ABABACC的情况
                            temp=len;
                            i--;
                            break;
                        }
                        if(k!=temp-1&&i==len-1){//主要针对ABCDAB的情况
                            temp=len;
                            break;
                        }
                        if((k==temp-1)&&(i!=len-1))
                            k=-1;//始终注意k的自加
                    }
                }
            }
            if(first){
                printf("%d
    ",temp);
                first=0;
            }
            else
                printf("
    %d
    ",temp);
        }
        return 0;
    }
    
  • 相关阅读:
    Cocos2d-x 学习笔记(15.2) EventDispatcher 事件分发机制 dispatchEvent(event)
    ‎Cocos2d-x 学习笔记(13) ActionEase
    ‎Cocos2d-x 学习笔记(12) Speed Follow
    ‎Cocos2d-x 学习笔记(11.10) Spawn
    ‎Cocos2d-x 学习笔记(11.9) FadeTo FadeIn FadeOut
    ‎Cocos2d-x 学习笔记(11.8) DelayTime ReverseTime TargetedAction ActionFloat Blink TintTo TintBy ResizeTo ResizeBy
    Cocos2d-x 学习笔记(11.7) Repeat RepeatForever
    Cocos2d-x 学习笔记(11.6) Sequence
    指针
    TX2常用命令
  • 原文地址:https://www.cnblogs.com/socrates-lzstu/p/5311884.html
Copyright © 2020-2023  润新知