最长不重叠子串
很容易就会让人想到后缀数组。我们前后做一个差,然后就是类似求最长不重叠子串了。
若两后缀的相同前缀长为i,那么最后就有i+1长的主题。
所以这里不光是不重叠,还要空一。
如何求解最长不重叠子串?二分,对height数组进行分组。(非常的巧妙!建议百度)
代码如下:
#include<iostream> #include<algorithm> #include<cmath> #include<math.h> #include<cstdio> using namespace std; const int max_n = 2e4 + 100; int a[max_n]; //后缀数组模板。自己写的太慢了呜呜 int ranks[max_n], SA[max_n], height[max_n]; int wa[max_n], wb[max_n], wvarr[max_n], wsarr[max_n]; inline int cmp(int* r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; } inline void get_sa(int* r, int* sa, int n, int m) { //r:原数组,我们此处默认为a //sa:SA //n:原数组长度 //m:原数组种类数,用于基数排序 ++n; int i, j, p, * x = wa, * y = wb, * t; for (i = 0; i < m; ++i) wsarr[i] = 0; for (i = 0; i < n; ++i) wsarr[x[i] = r[i]]++; for (i = 1; i < m; ++i) wsarr[i] += wsarr[i - 1]; for (i = n - 1; i >= 0; --i) sa[--wsarr[x[i]]] = i; for (j = 1, p = 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; for (i = 0; i < n; ++i) wvarr[i] = x[y[i]]; for (i = 0; i < m; ++i) wsarr[i] = 0; for (i = 0; i < n; ++i) wsarr[wvarr[i]]++; for (i = 1; i < m; ++i) wsarr[i] += wsarr[i - 1]; for (i = n - 1; i >= 0; --i) sa[--wsarr[wvarr[i]]] = y[i]; for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; ++i) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } } //求解高度数组,height[i]指排名 void get_height(int* r, int* sa, int n) { int i, j, k = 0; for (i = 1; i <= n; ++i) ranks[sa[i]] = i; for (i = 0; i < n; height[ranks[i++]] = k) for (k ? k-- : 0, j = sa[ranks[i] - 1]; r[i + k] == r[j + k]; k++); return; } void init(int n) { fill(a, a + n + 3, 0); fill(ranks, ranks + n + 3, 0); fill(SA, SA + n + 3, 0); fill(height, height + n + 3, 0); fill(wa, wa + n + 3, 0); fill(wb, wb + n + 3, 0); fill(wsarr, wsarr + n + 3, 0); fill(wvarr, wvarr + n + 3, 0); } //后缀数组 //附加rmq_st 专门对后缀数组 int st[max_n][32]; void initSt(int n) { for (int i = 0;i <= n;++i)st[i][0] = height[i]; int mxk = (int)log2(n + 1); for (int k = 1;k <= mxk;++k) { for (int i = 0;i <= n;++i) { if (i + (1 << k) - 1 > n)break; st[i][k] = min(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]); } } } int que(int l, int r) { l = ranks[l];r = ranks[r]; if (l > r)swap(l, r); ++l; int len = log2(r - l + 1); return min(st[l][len], st[r - (1 << len) + 1][len]); } int n; bool check(int len) { for (int i = 1, mi, ma;i <= n;++i) { if (height[i] < len - 1) { mi = SA[i];ma = SA[i]; } else { mi = min(min(SA[i], mi), SA[i - 1]); ma = max(max(SA[i], ma), SA[i - 1]); if (ma - mi >= len)return true; } }return false; } int ef() { int lft = 0;int rght = n + 1; while (lft < rght) { int mid = (lft + rght) >> 1; if (check(mid))lft = mid + 1; else rght = mid; }return rght - 1; } int main() { while (scanf("%d", &n)) { if (n == 0)break; init(n); for (int i = 0;i < n;++i)scanf("%d", &a[i]); for (int i = 0;i < n - 1;++i)a[i] = a[i + 1] - a[i]; for (int i = 0;i < n;++i)a[i] += 90; get_sa(a, SA, n, 300); get_height(a, SA, n); initSt(n);int ans = ef(); printf("%d\n", ans > 4 ? ans : 0); } }