【poj 1743】后缀数组

来源:互联网 发布:js 字段默认值 编辑:程序博客网 时间:2024/06/07 01:23

不可重叠最长重复子串。

二分答案后将height数组分段,看每段中sa最大减最小是否满足。

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>#include <string>#define Rep(i, x, y) for (int i = x; i <= y; i ++)#define RepE(i, x) for (int i = pos[x]; i; i = g[i].nex)#define Dwn(i, x, y) for (int i = x; i >= y; i --)using namespace std;typedef long long LL;const int N = 20005;int n, a[N], sa[N], t1[N], t2[N], c[N], rk[N], ht[N];bool Cmp(int *r, int x, int y, int l) { return r[x] == r[y] && r[x+l] == r[y+l]; }void Bsa(int *a, int n, int m) {int *x = t1, *y = t2, p = 0;Rep(i, 0, m) c[i] = 0;Rep(i, 0, n) c[x[i] = a[i]] ++;Rep(i, 1, m) c[i] += c[i-1];Rep(i, 0, n) sa[-- c[ x[i] ]] = i;for (int j = 1; p <= n; j <<= 1, m = p) {p = 0;Rep(i, n-j+1, n) y[p ++] = i;Rep(i, 0, n) if (sa[i] >= j) y[p ++] = sa[i] - j;Rep(i, 0, m) c[i] = 0;Rep(i, 0, n) c[ x[y[i]] ] ++;Rep(i, 1, m) c[i] += c[i-1];Dwn(i, n, 0) sa[-- c[ x[y[i]] ]] = y[i];swap(x, y), p = 1, x[ sa[0] ] = 0;Rep(i, 1, n) x[ sa[i] ] = Cmp(y, sa[i], sa[i-1], j) ? p - 1 : p ++;}}void Bh(int *a, int n) {Rep(i, 0, n) rk[ sa[i] ] = i;int k = 0;Rep(i, 0, n-1) {if (k) k --;int j = sa[ rk[i] - 1 ];while (a[i+k] == a[j+k]) k ++;ht[ rk[i] ] = k;}}int main(){while (~scanf ("%d", &n) && n != 0) {Rep(i, 0, n-1) scanf ("%d", &a[i]);Rep(i, 0, n-1) a[i] = a[i+1] - a[i] + 100;n --, a[n] = 0;Bsa(a, n, 190), Bh(a, n);int l = 0, r = n;while (l < r) {int mid = l + r + 1 >> 1, f = 0, m1 = 0, m2 = n+1;Rep(i, 1, n) {if (ht[i] >= mid) m1 = max(m1, sa[i]), m2 = min(m2, sa[i]);else m1 = sa[i], m2 = sa[i];if (m1 - m2 >= mid) f = 1;}if (f) l = mid;else r = mid - 1;}if (l < 4) puts("0");else printf("%d\n", l + 1);}    return 0;}


0 0
原创粉丝点击