B. power stringstime limit: 3000 msmemory limit: 65536kb64-bit integer Io format: % LLD Java class name: Main 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 ). inputeach 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. outputfor each s you shoshould print the largest N such that S = a ^ N for some string. sample Input
abcdaaaaababab.
Sample output
143
Hintthis problem has huge input, use scanf instead of CIN to avoid time limit exceed. Problem Solving: Find the length of the loop section of the string. Use the adaptive array of KMP. If the character length can be divisible (character length-fail [character length]), the cycle section is the operator. Otherwise, the cycle section length is 1, that is, the character itself.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 #define INF 0x3f3f3f11 using namespace std;12 const int maxn = 1000100;13 char str[maxn];14 int fail[maxn];15 void getFail(int &len) {16 int i,j;17 len = strlen(str);18 fail[0] = fail[1];19 for(i = 1; i < len; i++) {20 j = fail[i];21 while(j && str[j] != str[i]) j = fail[j];22 fail[i+1] = str[j] == str[i] ? j+1:0;23 }24 }25 int main() {26 int len;27 while(gets(str) && str[0] != ‘.‘) {28 getFail(len);29 if(len%(len-fail[len])) puts("1");30 else printf("%d\n",len/(len-fail[len]));31 }32 return 0;33 }
View code