D-periodic Strings (UVA-455)

Source: Internet
Author: User

A character string is said to has period K if it can be formed by concatenating one or more repetitions of anoth Er 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



It's a simple question, but I don't know why I did it all day, probably because I'm not in the state today. At the beginning of the general thought of a thought, but later found that the idea of a lot of errors, after various modifications began a bit of confusion, WA a few times after the note Gaga to find that when the input string is ASDFG such, I understand some deviation, The answer should be 5 instead of 0. The format of the output I did not see two output between a blank line, and later modified several times, or not, grabbed the code of the male to study a bit, changed a way of thinking, but there have been a lot of grammatical errors = Fortunately the last a dropped. Paste the code.

#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;
}

D-periodic Strings (UVA-455)

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.