题目:http://poj.org/problem?id=2406
题意:
给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的,
求 R 的最大值。
分析:
做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足。判断的时候,先看字符串 L 的长度能否被 k 整除,再看 suffix(1)和 suffix(k+1)的最长公共前缀是否等于 n-k。在询问最长公共前缀的时候,suffix(1)是固定的,所以 RMQ问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到height[rank[1]]之间的最小值即可。整个做法的时间复杂度为 O(n)。
倍增算法会超时,用DC3算法。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int INF = 1e9 + 9;
const int N = 3000000 + 9;//开大3倍
/********************DC3算法*后缀数组模板*******************************/
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[N], wb[N], wv[N], wss[N];
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);
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++) wss[i] = 0;
for (i = 0; i < n; i++) wss[wv[i]]++;
for (i = 1; i < m; i++) wss[i] += wss[i - 1];
for (i = n - 1; i >= 0; i--)
b[--wss[wv[i]]] = a[i];
}
void dc3 (int *r, int *sa, int n, int m) {
int i, j, *rn = r + n;
int *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++];
}
void da (int str[], int sa[], int rk[], int height[], int n, int m) {
dc3 (str, sa, n + 1, m);
int i, j, k = 0;
for (i = 0; i <= n; i++) rk[sa[i]] = i;
for (i = 0; i < n; i++) {
if (k) k--;
j = sa[rk[i] - 1];
while (str[i + k] == str[j + k]) k++;
height[rk[i]] = k;
}
}
/********************************************************************************/
int sa[N], rk[N], height[N], lcp[N], s[N];
char str[N];
int main() {
// freopen ("f.txt", "r", stdin);
while (~scanf ("%s", str) ) {
if (str[0] == '.') break;
int n = strlen (str);
for (int i = 0; i < n; i++) s[i] = str[i];
s[n] = 0;
da (s, sa, rk, height, n, 128);
lcp[rk[0]] = N;
for (int i = rk[0] - 1; i >= 0; i--) lcp[i] = min (lcp[i + 1], height[i + 1]);
for (int i = rk[0] + 1; i <= n; i++) lcp[i] = min (lcp[i - 1], height[i]);
for (int k = 1; k <= n; k++)
if (n % k == 0 && lcp[rk[k]] == n - k) {
printf ("%d\n", n / k);
break;
}
}
return 0;
}
/*
input
abcd
aaaa
ababab
.
output
1
4
3
*/
KMP算法也可以求解,准确的说是MP算法,这就要看对next数组的理解了~~
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int INF = 1e9 + 9;
const int N = 1000000 + 9;
int next[N];
void getNext (char t[], int tlen) {
int j, k;
j = 0;
k = next[0] = -1;
while (j < tlen) {
if (k == -1 || t[j] == t[k]) next[++j] = ++k;
else k = next[k];
}
}
char str[N];
int main() {
// freopen ("f.txt", "r", stdin);
while (~scanf ("%s", str) ) {
if (str[0] == '.') break;
int n = strlen (str);
getNext (str, n);
if (next[n] > 0 && n % (n - next[n]) == 0) printf ("%d\n", n / (n - next[n]) );
else puts ("1");
}
return 0;
}