3026-periodtime limit:3.000 seconds
For each prefix of a given string S with N characters (each character have an ASCII code between and 126, inclusive), we Want to know whether the prefix is a periodic string. That's, for each I (2≤i≤n) we want to know the largest K > 1 (if there are one) such that the prefix of S with Leng Th I can be written as AK, which is A concatenated K times, for some string a. Of course, we also want to know the period K.
Input
The input file consists of several test cases. Each test case consists of lines. The first one contains N (2≤n≤1) The size of the string S. The second line contains the string S. The input file ends with a line, has the number zero on it.
Output
For each test case, the output ' test Case # ' and the consecutive test case number on a single line; Then, for each prefix with length I that have a period K > 1, output the prefix size I and the period K separated by a s Ingle Space; The prefix sizes must is in increasing order. Print a blank line after each test case.
Sample Input
3aaa12aabaabaabaab0
Sample Output
Test Case #12 3Test case #22 26 29 312 4
I used the MP algorithm.
Obviously I can match the f[i], then the string is just going to be attached.
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (I,k,n) for (int. i=k;i<=n;i++) #define REP (I,n) for (int i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (I,n) for (int. i=n;i>=0;i--) #define FORP (x) for ( int p=pre[x];p; p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset ( A,128,sizeof (a)); #define INF (2139062143) #define F (100000007) #define MAXN (1000000+10) typedef long Long Ll;ll Mul (ll a,l L b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return (a-b+ (a)/f*f+f)%F; void Upd (ll &a,ll b) {a= (a%f+b%f)%F;} Kmpclass Kmp{public:int F2[MAXN];//String starting from 0, but F[i] to match the first character, leaving a f[0]--a-->f[1]--... such char T2[MAXN],P2[MAXN]; T is long,p is Model strvoid mem () {mem (F2) mem (T2) mem (P2)}int getfail (char *p=0,int* f=0) {if (p==0) p=p2;if (f==0) f=f2 ; int M=strlen (P); f[0]=f[1]=0; for (i,m-1) {int j=f[i];while (j&&p[i]!=p[j]) j=f[j];f[i+1]= p[i] = = P[j]? j+1:0;}} int find (char* t=0,char* p=0,int* f=0) {if (t==0) t=t2;if (p==0) p=p2;if (f==0) f=f2;int N=strlen (T), M=strlen (p); GetFail (P , f); int j=0; Rep (I,n) {while (J&&t[i]!=p[j]) j=f[j];if (T[i]==p[j]) j++;if (j==m) return i-m+1;}} S;int N;int Main () {//freopen ("la3026.in", "R", stdin),//freopen (". Out", "w", stdout); int Tt=0;while (scanf ("%d%s", &N,S.P2) {==2) {printf ("Test case #%d\n", ++TT); S.getfail (S.P2,S.F2); Fork (i,2,n) {if (i% (I-s.f2[i]) ==0&&s.f2[i]) printf ("%d%d\n", i,i/(I-s.f2[i])); } putchar (' \ n '); S.mem ();} return 0;}
LA 3026 (period-mp algorithm) [TEMPLATE:KMP]