Periodic Strings
| Time Limit: 3000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status
Description
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
Approximate test instructions: If a string can be repeated multiple times by a string of length k, it is said to have a period of K. The minimum period of the string to be given.
1#include <iostream>2#include <cstring>3 using namespacestd;4 intMain ()5 {6 intn,j;7Cin>>N;8 while(n--)9 {Ten Chara[ +]; One Charb[ +]; Ab[0]=' /'; -Cin>>A; - strcat (b,a); the strcat (b,a); - intnum=strlen (a); - for(intI=1; i<=num;i++) - { + intk=i; - if(strncmp (b+i,a,num-i) = =0&& Num%k = =0 ) + { Acout<<K; at Break; - } - } - if(j!=N) -cout<<endl<<Endl; - if(j==N) incout<<Endl; - } to return 0; +}
UVa 455 periodic Strings (cycle string)