UVa 455 periodic Strings: string handling

Source: Internet
Author: User
Tags integer time limit

455-periodic Strings

Time limit:3.000 seconds

http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=396

A character string is said to have period K if it can be formed by concatenating one one or more repetitions of another 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 (one repetition of "abcabcabcabc").

Write a program to read a character string and determine its smallest period.

Input

The the ' the ' OIF ' input file would contain a single-integer N indicating how-many test case that your program would TES T followed by a blank line. Each test case would contain a single character string of the up to Non-blank characters. Two consecutive input would separated by a blank line.

Output

An integer denoting the smallest period's the input string for each input. Two consecutive output are separated by a blank line.

Sample Input

1

Hohoho

Sample Output

2

WA anymore? There must be something wrong with IO!

Complete code:

/*0.028*/
    
#include <cstdio>  
#include <cstring>  
    
char word[100];  
    
int main (void)  
{  
    int n, Len;  
    scanf ("%d", &n);///be careful not to write "%d\n" because it ignores multiple \ n rather than one  
    getchar ();  
    while (n--)  
    {  
        getchar ();  
        Gets (word);  
        Len = strlen (word);  
        for (int i = 1; I <= len; i++)  
            if (len% i = = 0)  
            {  
                bool ok = true;  
                for (int j = i; J < Len; j +)  
                    if (word[j]!= word[j% i])  
                    {  
                        ok = false;  
                        break;  
                if (OK)  
                {  
                    printf ("%d\n", I);  
                    if (n) putchar (' \ n ');  
                    break;  
                }  
    }} return 0;  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.