题意:
求可重叠的最长重复子串,但有一个限制条件。。要至少重复k次
解析:
二分枚举k,对于连续的height 如果height[i] >= k 说明它们至少有k个元素是重复的,所以判断一下就好了
数据很水
输入数据可能为0,所以s[i]++ s[n++] = 0;
要后缀数组要保证末尾加的字符比前面的都小
这是百度百科上的。。这里的r数组即为s数组
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 50010, INF = 0x7fffffff; int a[maxn], s[maxn]; int sa[maxn], t[maxn], t2[maxn], c[maxn], n; int ran[maxn], height[maxn]; void get_sa(int m) { int i, *x = t, *y = t2; for(i = 0; i < m; i++) c[i] = 0; for(i = 0; i < n; i++) c[x[i] = s[i]]++; for(i = 1; i < m; i++) c[i] += c[i-1]; for(i = n-1; i >= 0; i--) sa[--c[x[i]]] = i; for(int k = 1; k <= n; k <<= 1) { int p = 0; for(i = n-k; i < n; i++) y[p++] = i; for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k; for(i = 0; i < m; i++) c[i] = 0; for(i = 0; i < n; i++) c[x[y[i]]]++; for(i = 0; i< m; i++) c[i] += c[i-1]; for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y); p = 1; x[sa[0]] = 0; for(i = 1; i < n; i++) x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++; if(p >= n) break; m = p; } int k = 0; for(i = 0; i < n; i++) ran[sa[i]] = i; for(i = 0; i < n; i++) { if(k) k--; int j = sa[ran[i]-1]; while(s[i+k] == s[j+k]) k++; height[ran[i]] = k; } } int len; bool solve(int k) { int cnt = 1; for(int i=1; i<n; i++) if(height[i] >= k) { cnt++; if(cnt>=len) return true; } else cnt = 1; return false; } int main() { rd(n); rd(len); rep(i, 0, n){ rd(s[i]); s[i]++; } s[n++] = 0; get_sa(20010); int l = 0, r = n; while(l <= r) { int mid = l + (r - l) / 2; if(solve(mid)) l = mid + 1; else r = mid - 1; } cout<< r <<endl; return 0; }