题意:
找出出现k次的可重叠的最长子串的长度
思路:
论文的一道题,就跟论文思路一样,二分长度,根据height将后缀分成很多组,如果有一组出现了k次以上,就存在这个长度的符合题意的字串。
错误及反思:
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 20100;
int sa[N],rak[N],height[N];
int arr[N];
void construct(const int *s,int n,int m=1000000) {static int t1[N],t2[N],c[1000010];int *x = t1,*y = t2;int i,j,k,p,l;memset(c,0,sizeof(c));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 (k = 1; k <= n; k <<= 1) {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 = 1; i < m; ++ i) c[i] += c[i - 1];for (i = n - 1; i >= 0; -- i) sa[--c[x[y[i]]]] = y[i];std::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;}for (i = 0; i < n; ++ i) rak[sa[i]] = i;for (i = 0,l = 0; i < n; ++ i) {if (rak[i]) {j = sa[rak[i] - 1];while (s[i + l] == s[j + l]) l++;height[rak[i]] = l;if (l) l--;}}
}
bool judge(int x,int n,int k)
{for(int i=2;i<=n;i++){int t=0;while(height[i]>=x){t++;i++;}if(t+1>=k) return true;}return false;
}
int main()
{int n,k;scanf("%d%d",&n,&k);for(int i=0;i<n;i++)scanf("%d",&arr[i]);construct(arr,n+1);int l=0,r=n+1;while(l+1<r){int m=(l+r)>>1;if(judge(m,n,k)) l=m;else r=m;}printf("%d\n",l);
}