Power Strings
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 36926 |
|
Accepted: 15254 |
Description
Given 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 are defined in the normal way:a^0 = "" (The empty string) and a^ (n+1) = A * (a^n).
Input
Each test case was a line of input representing S, a string of printable characters. The length of S'll be is at least 1 and would not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s should print the largest n such, that s = a^n for some string a.
Sample Input
Abcdaaaaababab.
Sample Output
143MP algorithm:
#include <stdio.h> #include <string.h> #define MAX 1100000char str[max];int f[max];void getfail ()//Mismatch function The array f defined by this function indicates that when the original string is mismatch with the target string { //can be based on f[] to jump to the corresponding position of the original string to improve the efficiency of the operation int i,j;int len=strlen (str); J=0;f[0]=f[1]=0;for (I=1 ; i<len;i++) {J=f[i];while (J && str[i]! = str[j]) j = f[j];f[i+1] = str[i] = = str[j]?j+1:0;}} int main () {int n,m,j,i,s,t;while (scanf ("%s", str) &&str[0]!= '. ') {getfail (); int len = strlen (str), if (len% (Len-f[len]))//here Len-f[len] means the length of the maximum loop section in the original string printf ("1\n"); len% (Len-f[len] Surplus number proves that the number of nodes removed from the maximum loop section still has //non-cyclic characters else printf ("%d\n", len/(Len-f[len]));// If the maximum loop section of the original character can represent the entire string , the output cycle will return 0;}
POJ 2406 Power Strings