給定一個字符串,求至少出現K次的最長重復子串,這k個子串可以重疊。
所謂出現K次就是在任意K個地方出現了這個子串,並不要求這K個是連續的。
那麼 根據羅大神的論文,用後綴數組解之
二分答案,然後用height數組判定是否至少出現了K個這種子串
[cpp]
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 20005
#define MAXM 1000005
#define INF 100000000000007LL
using namespace std;
int r[MAXN];
int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXM];
int sa[MAXN]; //index range 1~n value range 0~n-1
int cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *ws = tmp;
for (i = 0; i < m; i++) ws[i] = 0;
for (i = 0; i < n; i++) ws[x[i] = r[i]]++;
for (i = 1; i < m; i++) ws[i] += ws[i - 1];
for (i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i;
for (j = 1, p = 1; p < n; j *= 2, 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++) wv[i] = x[y[i]];
for (i = 0; i < m; i++) ws[i] = 0;
for (i = 0; i < n; i++) ws[wv[i]]++;
for (i = 1; i < m; i++) ws[i] += ws[i - 1];
for (i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i];
for (swap(x, y), 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++;
}
}
int rank[MAXN]; //index range 0~n-1 value range 1~n
int height[MAXN]; //index from 1 (height[1] = 0)
void calheight(int *r, int *sa, int n)
{
int i, j, k = 0;
for (i = 1; i <= n; ++i) rank[sa[i]] = i;
for (i = 0; i < n; height[rank[i++]] = k)
for (k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; ++k);
return;
}
int n, k;
bool check(int mid)
{
int cnt = 1;
for(int i = 1; i <= n; i++)
{
if(height[i] < mid) cnt = 1;
else cnt++;
if(cnt >= k) return 1;
}
return 0;
}
int main()
{
int m = 0;
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++)
{
scanf("%d", &r[i]);
r[i]++;
m = max(m, r[i]);
}
r[n] = 0;
da(r, sa, n + 1, m + 1);
calheight(r, sa, n);
int res = 0;
int left = 1, right = n;
while(left <= right)
{
int mid = (left + right) >> 1;
if(check(mid))
{
left = mid + 1;
res = max(res, mid);
}
else right = mid - 1;
}
printf("%d\n", res);
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#define eps 1e-5
#define MAXN 20005
#define MAXM 1000005
#define INF 100000000000007LL
using namespace std;
int r[MAXN];
int wa[MAXN], wb[MAXN], wv[MAXN], tmp[MAXM];
int sa[MAXN]; //index range 1~n value range 0~n-1
int cmp(int *r, int a, int b, int l)
{
return r[a] == r[b] && r[a + l] == r[b + l];
}
void da(int *r, int *sa, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *ws = tmp;
for (i = 0; i < m; i++) ws[i] = 0;
for (i = 0; i < n; i++) ws[x[i] = r[i]]++;
for (i = 1; i < m; i++) ws[i] += ws[i - 1];
for (i = n - 1; i >= 0; i--) sa[--ws[x[i]]] = i;
for (j = 1, p = 1; p < n; j *= 2, 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++) wv[i] = x[y[i]];
for (i = 0; i < m; i++) ws[i] = 0;
for (i = 0; i < n; i++) ws[wv[i]]++;
for (i = 1; i < m; i++) ws[i] += ws[i - 1];
for (i = n - 1; i >= 0; i--) sa[--ws[wv[i]]] = y[i];
for (swap(x, y), 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++;
}
}
int rank[MAXN]; //index range 0~n-1 value range 1~n
int height[MAXN]; //index from 1 (height[1] = 0)
void calheight(int *r, int *sa, int n)
{
int i, j, k = 0;
for (i = 1; i <= n; ++i) rank[sa[i]] = i;
for (i = 0; i < n; height[rank[i++]] = k)
for (k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; ++k);
return;
}
int n, k;
bool check(int mid)
{
int cnt = 1;
for(int i = 1; i <= n; i++)
{
if(height[i] < mid) cnt = 1;
else cnt++;
if(cnt >= k) return 1;
}
return 0;
}
int main()
{
int m = 0;
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++)
{
scanf("%d", &r[i]);
r[i]++;
m = max(m, r[i]);
}
r[n] = 0;
da(r, sa, n + 1, m + 1);
calheight(r, sa, n);
int res = 0;
int left = 1, right = n;
while(left <= right)
{
int mid = (left + right) >> 1;
if(check(mid))
{
left = mid + 1;
res = max(res, mid);
}
else right = mid - 1;
}
printf("%d\n", res);
return 0;
}