Power Strings
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 37660 | Accepted: 15576 |
Description
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcd aaaa ababab .
Sample Output
1 43
# include<stdio.h> # include<string.h> # include<algorithm> using namespace std; char s[1001000]; int p[1001000]; void get() { int i = 0, j = -1; p[0] = -1; int len = strlen(s); while(i < len) { if(s[i] == s[j] || j == -1) { i++; j++; p[i] = j; } else j = p[j]; } } int main() { int len; while(scanf("%s",s), s[0] != '.') { get(); len = strlen(s); //这个有点不清楚 就是上下两个语句不能调换位置 否则在oj过不去。 if(len % (len-p[len])) 如果结果不为零则说明有不属于循环体的内容。 printf("1\n"); else printf("%d\n", len/(len-p[len]));//<span style="font-family: 'Courier New', Courier, monospace;">len-p[len]是每个循环体的长度,用整个字节数除就是重复的次数。</span><span style="font-family: 'Courier New', Courier, monospace;"> </span>
} return 0; }