标签:
后缀数组的论文里的例题,论文里的题解并没有看懂,,,
求一个重复次数最多的连续重复子串,又因为要找最靠前的,所以扫的时候记录最大的重复次数为$ans$,扫完后再后从头暴力扫到尾找重复次数为$ans$的第一个子串的开头,break输出就可以了
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 100003; int t1[N], t2[N], c[N]; void st(int *x, int *y, int *sa, int n, int m) { int i; for(i = 0; i < m; ++i) c[i] = 0; for(i = 0; i < n; ++i) ++c[x[y[i]]]; for(i = 1; i < m; ++i) c[i] += c[i - 1]; for(i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i]; } void mkhz(int *a, int *sa, int n, int m) { int *x = t1, *y = t2, *t, i, j, p; for(i = 0; i < n; ++i) x[i] = a[i], y[i] = i; st(x, y, sa, n, m); for(p = 1, j = 1; p < n; j <<= 1, m = p) { for(p = 0, i = n - j; i < n; ++i) y[p++] = i; for(i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j; st(x, y, sa, n, m); for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; ++i) x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && y[sa[i] + j] == y[sa[i - 1] + j] ? p - 1 : p++; } } void mkh(int *r, int *sa, int *rank, int *h, int n) { int i, j, k = 0; for(i = 1; i <= n; ++i) rank[sa[i]] = i; for(i = 1; i <= n; h[rank[i++]] = k) for(k ? --k : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; ++k); } char s[N]; int a[N], rank[N], sa[N], h[N], n, pro = 0, f[N][30], ans, cnt, aa[N]; void mkst() { for(int i = 1; i <= n; ++i) f[i][0] = h[i]; int k = floor(log((double)n) / log(2.0)); for(int j = 1; j <= k; ++j) for(int i = 1; i <= n; ++i) { if (i + (1 << j) - 1 > n) break; f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]); } } int Q(int l, int r) { l = rank[l]; r = rank[r]; if (l > r) swap(l, r); ++l; int k = floor(log((double)(r - l + 1)) / log(2.0)); return min(f[l][k], f[r - (1 << k) + 1][k]); } int main() { while (scanf("%s", s + 1), s[1] != ‘#‘) { printf("Case %d: ", ++pro); n = strlen(s + 1); for(int i = 1; i <= n; ++i) a[i] = s[i]; mkhz(a, sa, n + 1, 130); mkh(a, sa, rank, h, n); mkst(); ans = 0, cnt = 0; for(int l = 1; l < n; ++l) for(int i = 1; i <= n - l; i += l) { int k = Q(i, i + l); int now = k / l + 1; int to = i - (l - k % l); if (to > 0 && k % l) if (Q(to, to + l) >= now) ++now; if (now > ans) { aa[cnt = 1] = l; ans = now; } else if (now == ans) { aa[++cnt] = l; } } int to = 0, len = 0; for(int i = 1; i <= n; ++i) for(int j = 1; j <= cnt; ++j) { int k = Q(sa[i], sa[i] + aa[j]); if (k >= (ans - 1) * aa[j]) { to = sa[i]; len = ans * aa[j]; i = n; break; } } for(int i = to; i < to + len; ++i) putchar(s[i]); puts(""); } return 0; }
终于A了233
【POJ 3693】Maximum repetition substring 重复次数最多的连续重复子串
标签:
原文地址:http://www.cnblogs.com/abclzr/p/5417966.html