题目链接:点击查看
题目大意:给出两个字符串,求出两个字符串中的最短公共子串,且在每个字符串中只出现过一次
题目分析:因为这个公共子串只能在字符串中出现一次,考虑到用后缀数组,我们先将两个字符串通过特殊字符拼接起来,求出后缀数组组后必须满足的一个条件就是height[ i ]必须大于height[ i - 1]和height[ i + 1 ]才行,这样就保证了sa[ i ]和sa[ i - 1 ]的公共子串肯定只出现了一次,其次必须满足sa[ i ]和sa[ i + 1 ]必须分别位于两个字符串之中,满足以上两个条件之后,维护一下max( height[ i - 1 ] , height[ i + 1 ] ) + 1 的最小值就是答案了
不过这个题目的特点一个是多串,一个是子串,显然可以用广义后缀自动机来做,直接将两个字符串添加到广义后缀自动机中,并且每次统计last的出现次数,拓扑序后直接统计就好了,显然后缀自动机处理起来效率更高,且更加无脑
代码:
后缀数组:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e4+100;
char str[N];
int sa[N]; //SA数组,表示将S的n个后缀从小到大排序后把排好序的
//的后缀的开头位置顺次放入SA中
int t1[N],t2[N],c[N];
int rk[N],height[N],len,belong[N];
int s[N];
void build_sa(int s[],int n,int m)//n为添加0后的总长
{
int i,j,p,*x=t1,*y=t2;
for(i=0;i<m;i++)
c[i]=0;
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(j=1;j<=n;j<<=1)
{
p=0;
for(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<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];
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]+j]==y[sa[i]+j]?p-1:p++;
if(p>=n)
break;
m=p;
}
}
void get_height(int s[],int n)//n为添加0后的总长
{
int i,j,k=0;
for(i=0;i<=n;i++)
rk[sa[i]]=i;
for(i=0;i<n;i++)
{
if(k)
k--;
j=sa[rk[i]-1];
while(s[i+k]==s[j+k])
k++;
height[rk[i]]=k;
}
}
void solve(int base=128)
{
build_sa(s,len+1,base);
get_height(s,len);
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
//#endif
// ios::sync_with_stdio(false);
len=0;
scanf("%s",str);
int len1=strlen(str);
for(int i=0;str[i];i++)
{
belong[len]=1;
s[len++]=str[i];
}
s[len++]=' ';
scanf("%s",str);
for(int i=0;str[i];i++)
{
belong[len]=2;
s[len++]=str[i];
}
s[len]=0;
solve();
int ans=inf;
for(int i=2;i<=len;i++)
{
if(belong[sa[i-1]]+belong[sa[i]]==3)
{
if(height[i-1]<height[i]&&height[i+1]<height[i])
ans=min(ans,max(height[i-1],height[i+1])+1);
}
}
if(ans==inf)
ans=-1;
printf("%d\n",ans);
return 0;
}
广义后缀自动机:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=1e4+100;
char s[N];
int tot=1,last=1,id[N<<1],tong[N<<1],cnt[N<<1][2];
struct Node
{
int ch[26];
int fa,len;
}st[N<<1];
void add(int x)
{
int p=last;
//
if(st[p].ch[x])
{
int q=st[p].ch[x];
if(st[q].len==st[p].len+1)
last=q;
else
{
int np=last=++tot;
st[np].len=st[p].len+1;
st[np].fa=st[q].fa;
st[q].fa=np;
for(int i=0;i<26;i++)
st[np].ch[i]=st[q].ch[i];
while(st[p].ch[x]==q)
st[p].ch[x]=np,p=st[p].fa;
}
return;
}
//
int np=last=++tot;
st[np].len=st[p].len+1;
while(p&&!st[p].ch[x])st[p].ch[x]=np,p=st[p].fa;
if(!p)st[np].fa=1;
else
{
int q=st[p].ch[x];
if(st[p].len+1==st[q].len)st[np].fa=q;
else
{
int nq=++tot;
st[nq]=st[q]; st[nq].len=st[p].len+1;
st[q].fa=st[np].fa=nq;
while(p&&st[p].ch[x]==q)st[p].ch[x]=nq,p=st[p].fa;//向上把所有q都替换成nq
}
}
}
void radix_sort()
{
memset(tong,0,sizeof(tong));
for(int i=1;i<=tot;i++)
tong[st[i].len]++;
for(int i=1;i<=tot;i++)
tong[i]+=tong[i-1];
for(int i=1;i<=tot;i++)
id[tong[st[i].len]--]=i;
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
//#endif
// ios::sync_with_stdio(false);
scanf("%s",s);
for(int i=0;s[i];i++)
{
add(s[i]-'a');
cnt[last][0]++;
}
last=1;
scanf("%s",s);
for(int i=0;s[i];i++)
{
add(s[i]-'a');
cnt[last][1]++;
}
radix_sort();
int ans=inf;
for(int i=tot;i>1;i--)
{
int cur=id[i],fa=st[cur].fa;
if(cnt[cur][0]==1&&cnt[cur][1]==1)
ans=min(ans,st[fa].len+1);
cnt[fa][0]+=cnt[cur][0];
cnt[fa][1]+=cnt[cur][1];
}
if(ans==inf)
ans=-1;
printf("%d\n",ans);
return 0;
}