Language:DefaultPower strings
| Time limit:3000 Ms |
|
Memory limit:65536 K |
| Total submissions:33335 |
|
Accepted:13852 |
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 shoshould print the largest N such that S = a ^ N for some string.Sample Input abcdaaaaababab. Sample output 143 Hint This problem has huge input, use scanf instead of CIN to avoid time limit exceed.Source Waterloo local 2002.07.01 |
Returns the minimum cycle of a string.
For a string, if abcdabc is next [Len] = 3, then Len-next [Len] is greater than Len/2, then Len % (LEN-next [Len])! = 0; for a periodic string abababab next [Len] = 4, len-next [Len] should be equal
The minimum length of the string, so if there is a minimum cycle, you can use Len % (LEN-next [Len]) to determine whether it is 0 (in my opinion, if there is a mistake, please tell me)
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<stack>#include<vector>#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8using namespace std;#define N 100005char a[N];int len,next[N];void getfail(char *a){ int i,j; len=strlen(a); i=0;j=-1; next[0]=-1; while(i<len) { if(j==-1||a[i]==a[j]){ i++; j++; next[i]=j;}elsej=next[j]; }}int main(){int i,j;while(scanf("%s",a)){if(a[0]=='.') break;getfail(a);int ans=len%(len-next[len]);if(ans==0)printf("%d\n",len/(len-next[len]));elseprintf("1\n");}return 0;}
Poj 2406 power strings (KMP)