POJ2774
链接
后缀数组做法
题意求两个串的最长公共子串,我们可以把两个串用一个特殊字符拼接,然后跑
SA求出height
S
A
求
出
h
e
i
g
h
t
,由于最长公共子串肯定是
height
h
e
i
g
h
t
中的某一个,而且要满足
sa[i]和sa[i−1]
s
a
[
i
]
和
s
a
[
i
−
1
]
分别属于两个串,所以只要跑一遍后缀数组遍历一遍
height
h
e
i
g
h
t
就好了。
POJ2774代码
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define maxn 200005
int wa[maxn],wb[maxn],wsf[maxn],wv[maxn],sa[maxn];
int rank[maxn],height[maxn],s[maxn];
char str1[maxn],str2[maxn];
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; i>=0; i--) sa[--wsf[x[i]]]=i;
p=1;
j=1;
for(; p<=n; j*=2,m=p)
{
for(p=0,i=n+1-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; 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 getheight(int *r,int n)
{
int i,j,k=0;
for(i=1; i<=n; i++) rank[sa[i]]=i;
for(i=0; i<n; i++)
{
if(k)
k--;
else
k=0;
j=sa[rank[i]-1];
while(r[i+k]==r[j+k])
k++;
height[rank[i]]=k;
}
}
int main()
{
int len,n;
while(~scanf("%s%s",str1,str2))
{
n=0;
len=strlen(str1);
for(int i=0;i<len;i++)
s[n++]=str1[i]-'a'+1;
s[n++]=30;
len=strlen(str2);
for(int i=0;i<len;i++)
s[n++]=str2[i]-'a'+1;
s[n]=0;//两串拼接末尾添0
getsa(s,sa,n,31);
getheight(s,n);
len=strlen(str1);
int ans=0;
for(int i=2;i<=n-1;i++)
{
if(height[i]>ans)
{
if(sa[i-1]>=0&&sa[i-1]<len&&sa[i]>len)
ans=max(ans,height[i]);
if(sa[i]>=0&&sa[i]<len&&sa[i-1]>len)
ans=max(ans,height[i]);
}
}
printf("%d\n",ans);
}
return 0;
}
Hash做法
由于没有给定长度,要求长度,这时就要想是否具有二分的性质,发现答案是具有二分性质的,所以我们可以二分答案,然后把A串中所有出现过的hash值放进一个数组,sort一下,然后对于每个B串产生的hash用lower_bound查询是否出现过,若出现过则直接返回true.复杂度是
o(len∗log(len)∗log(len))
o
(
l
e
n
∗
l
o
g
(
l
e
n
)
∗
l
o
g
(
l
e
n
)
)
,跑了1250ms,用map判断是否出现过就会超时,这暂且作为hash的一个优化方式吧。
POJ2774代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
typedef unsigned long long ull;
const int maxn = 1e6+5;
ull xp[maxn],hash_1[maxn],hash_2[maxn];
ull a[maxn];
int len1,len2;
void init()
{
xp[0]=1;
for(int i=1;i<maxn;i++)
xp[i]=xp[i-1]*13331;
}
ull get_hash(int i,int L,ull hash_[])
{
return hash_[i]-hash_[i+L]*xp[L];
}
int make_hash(char str[],ull hash_[])
{
int len=strlen(str);
hash_[len]=0;
for(int i=len-1;i>=0;i--)
{
hash_[i]=hash_[i+1]*13331+(str[i]-'a'+1);
}
return len;
}
char str[maxn],str2[maxn];
bool check(int L)
{
int cnt=0;
for(int i=0;i<len1-L+1;i++)
{
a[cnt++]=get_hash(i,L,hash_1);
}
sort(a,a+cnt);
for(int i=0;i<len2-L+1;i++)
{
ull tmp=get_hash(i,L,hash_2);
int pos=lower_bound(a,a+cnt,tmp)-a;
if(a[pos]==tmp) return true;
}
return false;
}
int main()
{
init();
while( scanf("%s%s",str,str2)!=EOF)
{
len1=make_hash(str,hash_1);
len2=make_hash(str2,hash_2);
int l=0,r=min(len1,len2),mid;
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid)) l=mid+1;
else r=mid-1;
}
printf("%d\n",r);
}
return 0;
}