POJ 2406 KMP/后缀数组
题目链接:http://poj.org/problem?id=2406
题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数]
思路一:KMP求最小循环机,然后就能求出循环次数。
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<queue> #include<vector> #include<time.h> #include<cmath> using namespace std; typedef long long int LL; const int MAXN = 1000000 + 5; char str[MAXN]; int Next[MAXN],len; void getNext(){ int i=0, k = -1; Next[0] = -1; while (i < len){ if (k == -1 || str[i] == str[k]){ ++i; ++k; Next[i] = k; } else{ k = Next[k]; } } } int main(){ //#ifdef kirito // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); //#endif // int start = clock(); while (scanf("%s", str) && str[0] != '.'){ len = strlen(str); getNext(); if (Next[len] && (len % (len - Next[len])== 0)){ printf("%d\n", len / (len-Next[len])); } else{ printf("1\n"); } } //#ifdef LOCAL_TIME // cout << "[Finished in " << clock() - start << " ms]" << endl; //#endif return 0; }
思路二:后缀数组,直接根据09年oi论文<<后缀数组——出来字符串的有力工具>>的解法。
穷举字符串S 的长度k,然后判断是否满足。判断的时候,先看字符串L 的长度能否被k 整除,再看suffix(1)和suffix(k+1)的最长公共前缀是否等于n-k。在询问最长公共前缀的时候,suffix(1)是固定的,所以RMQ问题没有必要做所有的预处理, 只需求出height 数组中的每一个数到height[rank[1]]之间的最小值即可。整个做法的时间复杂度为O(n)。
补充:该题字符串长度比较大,达到1e7的上限,所以O(nlogn)的倍增会TLE,所以考虑O(n)的DC3。 但是还是要2500ms才能AC,对于KMP的125ms来说,该题还是KMP比较优而且代码了比较少,不过学习到求最小循环次数还可以用后缀数组来做。
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #include<queue> #include<vector> #include<time.h> #include<cmath> using namespace std; typedef long long int LL; #define INF 0x3f3f3f3f #define F(x) ((x)/3+((x)%3==1?0:tb)) #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2) const int MAXN = 10000000 + 5; int wa[MAXN], wb[MAXN], wv[MAXN], WS[MAXN]; int c0(int *r, int a, int b) { return 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], sa[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; } int len, r[MAXN], LCP[MAXN]; char str[MAXN]; void solve(){ //LCP[i]:suffix(0)和suffix(i)的最长公共前缀 for (int i = Rank[0] - 1, lcpval = INF; i > 0; i--){ lcpval = min(lcpval, height[i + 1]); LCP[sa[i]] = lcpval; } for (int i = Rank[0] + 1, lcpval = INF; i <= len; i++){ lcpval = min(lcpval, height[i]); LCP[sa[i]] = lcpval; } int ans = 1; for (int k = 1; k <= len; k++){ if (len%k != 0){ continue; } if (LCP[k] == len - k){ //第一个找到一定是最优解 ans = len / k; break; } } printf("%d\n", ans); } int main(){ //#ifdef kirito // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); //#endif // int start = clock(); while (scanf("%s", str) && str[0] != '.'){ len = strlen(str); for (int i = 0; i <= len; i++){ if (i == len){ r[i] = 0; continue; } r[i] = (int)str[i]; } dc3(r, sa, len + 1, 256); calheight(r, sa, len); solve(); } //#ifdef LOCAL_TIME // cout << "[Finished in " << clock() - start << " ms]" << endl; //#endif return 0; }