題目鏈接:點擊查看
題目大意:給出一個字符串 s ,再給出 n 個字符串 t ,現在問字符串 s 中有多少個不同的子串滿足不是 t1 ~ tn 中任意一個字符串的子串
題目分析:第一次接觸這樣的題目,一開始還是有點手足無措的,不過跟着題解學了一波之後稍微有了點思路,這個題目是有兩種解法的,比較簡單的一種是可以構建多串後綴自動機,首先將 n 個字符串 t 構建後綴自動機,注意每次只需要將 last 初始化爲 root 即可,求出這時的本質不同子串的個數 ans1,然後再將字符串 s 也加入到後綴自動機中,求出此時本質不同子串的個數 ans2,顯然答案就是 ans2 - ans1 了
上面的解法比較好實現,還有一種方法,是可以將後綴自動機當做AC自動機來用,先將字符串 s 建立後綴自動機,然後讓 n 個字符串 t 在後綴自動機上匹配,記錄下每個節點可以匹配的最大長度,假設某個節點可以匹配的最大長度爲 p[ i ],其原本可以貢獻的本質不同的子串個數爲 len[ i ] - len[ fa[ i ] ] 的,現在就變成了 len[ i ] - max( len[ fa[ i ] ] , p[ i ] ) 了,拓撲排序之後從大到小計算一下貢獻就好了
2020.2.14更新:
話說多串後綴自動機不就是廣義後綴自動機嘛。。怪我孤陋寡聞了
代碼:
廣義後綴自動機:
#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=2e5+100;
char s[N],str[N];
int tot,last,id[N<<1],tong[N<<1];
struct Node
{
int ch[26];
int fa,len;
}st[N<<1];
void newnode()
{
tot++;
for(int i=0;i<26;i++)
st[tot].ch[i]=0;
st[tot].fa=st[tot].len=0;
}
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
{
newnode();
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;
}
//
newnode();
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 init()
{
tot=0,last=1;
newnode();
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
//#endif
// ios::sync_with_stdio(false);
int w;
cin>>w;
int kase=0;
while(w--)
{
init();
int n;
scanf("%d%s",&n,str);
while(n--)
{
last=1;
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++)
add(s[i]-'a');
}
LL ans1=0;
for(int i=1;i<=tot;i++)
ans1+=st[i].len-st[st[i].fa].len;
int len=strlen(str);
last=1;
for(int i=0;i<len;i++)
add(str[i]-'a');
LL ans2=0;
for(int i=1;i<=tot;i++)
ans2+=st[i].len-st[st[i].fa].len;
printf("Case %d: %lld\n",++kase,ans2-ans1);
}
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=1e5+100;
char s[N];
int tot,last,id[N<<1],tong[N<<1];
struct Node
{
int ch[26];
int fa,len,p;
}st[N<<1];
void newnode()
{
tot++;
for(int i=0;i<26;i++)
st[tot].ch[i]=0;
st[tot].fa=st[tot].len=st[tot].p=0;
}
void add(int x)
{
newnode();
int p=last,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 search()
{
int len=strlen(s);
int pos=1,l=0;
for(int i=0;i<len;i++)
{
int to=s[i]-'a';
if(st[pos].ch[to])
{
pos=st[pos].ch[to];
l++;
}
else
{
while(pos!=1&&!st[pos].ch[to])
{
pos=st[pos].fa;
l=st[pos].len;
}
if(st[pos].ch[to])
{
pos=st[pos].ch[to];
l++;
}
else
pos=1,l=0;
}
st[pos].p=max(st[pos].p,l);
}
}
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;
}
void init()
{
tot=0,last=1;
newnode();
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
//#endif
// ios::sync_with_stdio(false);
int w;
cin>>w;
int kase=0;
while(w--)
{
init();
int n;
scanf("%d%s",&n,s);
int len=strlen(s);
for(int i=0;i<len;i++)
add(s[i]-'a');
while(n--)
{
scanf("%s",s);
search();
}
radix_sort();
LL ans=0;
for(int i=tot;i>=1;i--)
{
int cur=id[i],fa=st[cur].fa;
st[fa].p=max(st[fa].p,st[cur].p);
if(st[cur].p<st[cur].len)
ans+=st[cur].len-max(st[fa].len,st[cur].p);
}
printf("Case %d: %lld\n",++kase,ans);
}
return 0;
}