Exercise 3-4 Cycle String UVa455

Source: Internet
Author: User

Description

A character string is said to has period K If it can be formed by concatenating one or more repetitions of another string of length k. For example, the string "abcabcabcabc" had period 3, since it was formed by 4 repetitions of the string "abc ". It also has periods 6 (both repetitions of "abcabc") and (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 would contain a single integer N indicating what many test case which your program would TES T followed by a blank line. Each of the test case would contain a single character string of Non-blank characters. Consecutive input would separated by a blank line.

Output

An integer denoting the smallest period of the input string for each input. The consecutive output is separated by a blank line.

Sample Input
1HoHoHo
Sample Output

 2

Code Implementation

#include <stdio.h>#include<string.h>intIsloopstring (CharStr[],intAintb);intMain () {intn,i,k; Charstr[ -]; 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); Elseprintf ("%d\n\n", K); }    return 0;}intIsloopstring (CharStr[],intAintb) {    inti,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;}

Exercise 3-4 Cycle string UVa455

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.