This problem can be done with the suffix array, KMP method
Suffix array practice began to think of the solution, the method is to enumerate the string length len of the approximate K, see LCP (suffix (0), suffix (k)) length is n-k, if True len/k is the result.
If the length of the LCP (suffix (0), suffix (k)) is N-k, the string is divided into a section of each K-bit, then the 1th and 2nd paragraphs can be matched, and the 2nd and 3rd segments can be pushed to match ... Always recursion, that every K-bit is the same, drawing can see that the matching process is similar to the Serpentine.
Use the multiplication algorithm to time out, with the DC3 algorithm 2.5 seconds reluctantly.
#include <cstdio> #include <cstring>using namespace std; #define MAXN 1000108int SA[MAXN * 3];//r[] and sa[] Array to open three times times the size of int LCP[MAXN]; #define F (x) ((x)/3+ ((x)%3==1?0:TB) #define G (x) ((x) <TB? ( x) *3+1: ((x)-TB) *3+2) #define MIN (A, B) (a < b?a:b) int wa[maxn],wb[maxn],wv[maxn],ws[maxn];int c0 (int *r,int a,int b) {R Eturn r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];} int C12 (int k,int *r,int A,int b) {if (k==2) return r[a]<r[b]| | R[A]==R[B]&&C12 (1,r,a+1,b+1); else Return r[a]<r[b]| | R[A]==R[B]&&WV[A+1]<WV[B+1];} void sort (int *r,int *a,int *b,int n,int m) {int i; for (i=0;i<n;i++) wv[i]=r[a[i]]; for (i=0;i<m;i++) ws[i]=0; for (i=0;i<n;i++) ws[wv[i]]++; for (i=1;i<m;i++) ws[i]+=ws[i-1]; for (i=n-1;i>=0;i--) b[--ws[wv[i]]]=a[i]; return;} void dc3 (int *r,int *sa,int n,int m) {int i,j,*rn=r+n,*san=sa+n,ta=0,tb= (n+1)/3,tbc=0,p; r[n]=r[n+1]=0; for (i=0;i<n;i++) if (i%3!=0) wa[tbc++]=i; Sort (r+2,wa,wb,tbc,m); Sort (r+1,wb,wa,tbc,m); Sort (r,wa,wb,tbc,m); For (P=1,rn[f (wb[0])]=0,i=1;i<tbc;i++) rn[f (Wb[i])]=c0 (R,wb[i-1],wb[i])? p-1:p++; if (P<TBC) DC3 (rn,san,tbc,p); else for (i=0;i<tbc;i++) san[rn[i]]=i; for (i=0;i<tbc;i++) if (SAN[I]<TB) wb[ta++]=san[i]*3; if (n%3==1) wb[ta++]=n-1; Sort (r,wb,wa,ta,m); for (i=0;i<tbc;i++) wv[wb[i]=g (san[i])]=i; For (I=0,j=0,p=0;i<ta && j<tbc;p++) sa[p]=c12 (Wb[j]%3,r,wa[i],wb[j])? wa[i++]:wb[j++]; for (; i<ta;p++) sa[p]=wa[i++]; for (; j<tbc;p++) sa[p]=wb[j++]; return;} int rank[maxn],height[maxn];void calheight (int *r,int *sa,int n) {int i,j,k=0; for (i=1;i<=n;i++) rank[sa[i]]=i; for (i=0;i<n;height[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++); return;} Char str[maxn];int R[MAXN * 3];int hs[1000];int Main () {while (~SCANF ("%s", str) && str[0]! = '. ') {int len = 0; int i; memset (HS, 0, sizeof (HS)); int cnt = 0; For (i = 0; str[i]; i++) {if (Hs[str[i]] = 0) {Hs[str[i]] = ++cnt; } R[i] = Hs[str[i]]; } len = i; R[len] = 0; DC3 (R, SA, Len + 1, cnt + 1); Calheight (R, SA, Len); Lcp[rank[0]] = len; for (int i = rank[0] + 1; I <= len; i++) {Lcp[i] = min (lcp[i-1], height[i]); } for (int i = rank[0]-1; I >= 1; i--) {Lcp[i] = min (lcp[i + 1], Height[i + 1]); } int ans = len; for (int i = 1; I <= len/2; i++) {if (len% i = = 0) {if (lcp[rank[i]] = = Len-i) {ans = i; Break }}} printf ("%d\n", Len/ans); } return 0;}
After the Epiphany KMP approach, the principle and similar, but the efficiency is much higher, more than 100 milliseconds.
#include <cstdio> #include <iostream> #include <cstdlib> #include <cstring> #include <string > #include <algorithm> #include <map> #include <queue> #include <vector> #include <cmath > #include <utility>using namespace std;typedef long long ll;const int N = 1000008, INF = 0x3f3f3f3f; #define MS (A, num) memset (A, num, sizeof (a)) #define PB (a) push_back (a) #define for (I, n) for (int i = 0; i < n; i++) char Str[n];int f[n ];void getf (char p[]) {f[0]=-1; int i = 0, j =-1; while (P[i]) {if (j = =-1 | | P[i]==p[j]) {i++; j + +; F[i] = j; }else{J=f[j]; }}}int Main () {while (~SCANF ("%s", str) && str[0]! = '. ') {GETF (str); int len = strlen (str); int a = len-1; int b = F[a]; if (a && str[a] = = str[b] && len% (A-b) = = 0) {printf ("%d\n", Len/(A-B)); }else{printf ("1\n"); }} return 0;}
POJ2406 Power Strings (KMP, suffix array)