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/