題目鏈接:點擊查看
題目大意:給出兩個字符串,求出兩個字符串中的最短公共子串,且在每個字符串中只出現過一次
題目分析:因爲這個公共子串只能在字符串中出現一次,考慮到用後綴數組,我們先將兩個字符串通過特殊字符拼接起來,求出後綴數組組後必須滿足的一個條件就是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;
}