//是看了别人的代码才AC的。我的代码是有问题的
先 上我的代码
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=10000*2+20;
int wa[maxn],wb[maxn],wsf[maxn],wv[maxn],sa[maxn];
int rank[maxn],height[maxn],s[maxn];
char t[20010];
int cmp(int *r,int a,int b,int k)
{
return r[a]==r[b]&&r[a+k]==r[b+k];
}
void getsa(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++)
{
wsf[i]=0;
}
for(i=0;i<n;i++)
{
wsf[x[i]=r[i]]++;
}
for(i=1;i<m;i++)
{
wsf[i]+=wsf[i-1];
}
for(i=n-1;i>=0;i--)
{
sa[--wsf[x[i]]]=i;
}
p=1;j=1;
for(;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++) wsf[i]=0;
for(i=0;i<n;i++) wsf[wv[i]]++;
for(i=1;i<m;i++) wsf[i]+=wsf[i-1];
for(i=n-1;i>=0;i--) sa[--wsf[wv[i]]]=y[i];
t=x;
x=y;
y=t;
x[sa[0]]=0;
for(p=1,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;
}
}
void geth(int *r,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 main()
{
int casenum;
scanf("%d",&casenum);
getchar();
while(casenum--)
{
int i,n=0;
gets(t);
int chang=strlen(t);
// cout<<chang<<endl;
t[chang]='#';
for(i=0;i<chang;i++)
{
s[n++]=t[i]-(char)(32)+1;
//cout<<s1[i];
}
s[n++]=0;
gets(t+chang+1);
int chang2=strlen(t);
// cout<<chang<<endl;
// cout<<(char)(32)<<endl;
for(i=n;i<chang2;i++)
{
//cout<<t[i];
s[n++]=t[i]-(char)(32)+1;
}
s[n]=0;
getsa(s,sa,n+1,180);
// cout<<"haha"<<endl;
geth(s,n);
int ans=0;
for(i=1;i<=n;i++)
{
if((sa[i]<chang)!=(sa[i-1]<chang))
{
ans=max(ans,height[i]);
//cout<<ans<<endl;
}
}
cout<<"Nejdelsi spolecny retezec ma delku "<<ans<<'.'<<endl;
}
return 0;
}
在上别人的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 300000;
int n,k,_rank[maxn],sa[maxn],lcp[maxn],tmp[maxn];
char ss[100010];
bool cmp_sa(int i,int j){
if(_rank[i] != _rank[j]) return _rank[i] < _rank[j];
int ri = i+k <= n ? _rank[i+k]:-1;
int rj = j+k <= n ? _rank[j+k]:-1;
return ri < rj;
}
void construct_sa(char *S,int *sa){
for(int i = 0; i <= n; i++){
sa[i] = i;
_rank[i] = i < n ? S[i]:-1;
}
for(k = 1; k <= n; k <<= 1){
sort(sa,sa+n+1,cmp_sa);
tmp[sa[0]] = 0;
for(int i = 1; i <= n; i++)
tmp[sa[i]] = tmp[sa[i-1]] + cmp_sa(sa[i-1],sa[i]);
for(int i = 0; i <= n; i++)
_rank[i] = tmp[i];
}
}
void construct_lcp(char *S,int *lcp){
for(int i = 0; i <= n; i++) _rank[sa[i]] = i;
int h = lcp[0] = 0;
for(int i = 0; i < n; i++){
if(h) h--;
for(int j = sa[_rank[i]-1]; i+h < n && j+h < n && S[i+h] == S[j+h]; ++h);
lcp[_rank[i]-1] = h;
}
}
int main() {
int t,slen;
scanf("%d",&t);
getchar();
while(t--){
gets(ss);
slen = strlen(ss);
//cout<<slen<<endl;
ss[slen] = '#';
gets(ss+slen+1);
n = strlen(ss);
//cout<<n<<endl;
memset(sa,0,sizeof(sa));
memset(lcp,0,sizeof(lcp));
construct_sa(ss,sa);
construct_lcp(ss,lcp);
int ans = 0;
for(int i = 0; i < n; i++)
if((sa[i] < slen) != (sa[i+1] < slen)) ans = max(ans,lcp[i]);
printf("Nejdelsi spolecny retezec ma delku %d.\n",ans);
}
return 0;
}