题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4080
题意:给出一个串s,求s的一个最长子串t,使得t在s中至少出现m次?
思路:二分。分组。
int r[N],sa[N],wa[N],wb[N],wd[N],rank[N],h[N];
int cmp(int *r,int a,int b,int len)
{
return r[a]==r[b]&&r[a+len]==r[b+len];
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
FOR0(i,m) wd[i]=0;
FOR0(i,n) wd[x[i]=r[i]]++;
FOR1(i,m-1) wd[i]+=wd[i-1];
FORL0(i,n-1) sa[--wd[x[i]]]=i;
for(j=1,p=1;p<n;j<<=1,m=p)
{
p=0;
FOR(i,n-j,n-1) y[p++]=i;
FOR0(i,n) if(sa[i]>=j) y[p++]=sa[i]-j;
FOR0(i,m) wd[i]=0;
FOR0(i,n) wd[x[i]]++;
FOR1(i,m-1) wd[i]+=wd[i-1];
FORL0(i,n-1) sa[--wd[x[y[i]]]]=y[i];
t=x;x=y;y=t;p=1;x[sa[0]]=0;
FOR1(i,n-1) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
void calHeight(int *r,int *sa,int n)
{
int i,j,k=0;
FOR1(i,n) rank[sa[i]]=i;
FOR0(i,n)
{
if(k) k--;
j=sa[rank[i]-1];
while(i+k<n&&j+k<n&&r[i+k]==r[j+k]) k++;
h[rank[i]]=k;
}
}
int n,m;
char s[N];
int start;
int OK(int x)
{
start=-INF;
int i,j,L,R,flag=0;
FOR1(i,n)
{
L=i;
while(L<=n&&h[L]<x) L++;
if(L>n) break;
R=L;
while(R<=n&&h[R]>=x) R++;
if(R-L+1>=m)
{
FOR(j,L-1,R-1) start=max(start,sa[j]);
flag=1;
}
i=R;
}
return flag;
}
void deal()
{
if(m==1)
{
PR(n,0);
return;
}
int low=1,high=n,mid;
while(low<=high)
{
mid=(low+high)>>1;
if(OK(mid)) low=mid+1;
else high=mid-1;
}
if(low<=n&&OK(low)) PR(low,start);
else if(high>=1&&OK(high)) PR(high,start);
else puts("none");
}
int main()
{
while(scanf("%d",&m),m)
{
RD(s);
int L=strlen(s),i;
FOR0(i,L) r[i]=s[i];
r[L]=0;
da(r,sa,L+1,130);
calHeight(r,sa,L);
n=strlen(s);
deal();
}
return 0;
}