本文共 1035 字,大约阅读时间需要 3 分钟。
后缀数组 的入门题。
先计算好SA和height数组。
然后二分答案。二分的时候,对height进行分组,看存不存在一组的大小大于等于k
//// main.cpp// 后缀数组//// Created by liuzhe on 17/2/5.// Copyright © 2017年 my_code. All rights reserved.//#include#include #include #include #include using namespace std;const int maxn = 50010;int wa[maxn],wb[maxn],wv[maxn],ww[maxn];int sa[maxn],lcp[maxn],rank[maxn],rank1[maxn];int str[maxn];int t,nn;inline bool cmp(int *r,int a,int b,int len){ return r[a]==r[b]&&r[a+len]==r[b+len];}void construct_sa(int n,int m){ int i,j,p,*x=wa,*y=wb,*t; n++;//why? for(i=0;i =0;i--) sa[--ww[x[i]]] = i; for(j=p=1;p <<=1,m=p) { for(p=0,i=n-j;i =j) y[p++] = sa[i]-j; } for(i=0;i =0;i--) sa[--ww[wv[i]]] = y[i]; for(t=x,x=y,y=t,x[sa[0]]=0,p=i=1;i 0) h--; for(;j+h =mid&&i =t-1) { flag=1;break; } } if(flag) return 1; else return 0;}int main(){ while(scanf("%d%d",&nn,&t)!=-1) { for(int i=0;i 1) { int mid=(le+ri)>>1; if(judge(mid,nn)) le=mid; else ri=mid; } printf("%d\n",le); } return 0;}
转载地址:http://vjali.baihongyu.com/