題目鏈接
\(Description\)
給定n個模式串,多次詢問一個串在多少個模式串中出現過。(字符集為26個小寫字母)
\(Solution\)
對每個詢問串進行匹配最終會達到一個節點,我們需要得到這個節點所代表的子串出現在多少個模式串中。
建立廣義后綴自動機。每次插入一個串,從root開始,對于SAM上每個節點維護cnt和bef,分別表示該節點代表的串出現在幾個模式串中 和 該節點最近被哪個模式串更新過cnt。
對于bef[i]!=now的節點,++cnt[i],bef[i]=now;當模式串now下次匹配到當前節點時則不再更新。
另外,如果匹配了當前節點i那么一定會匹配上fa[i],fa[fa[i]]...如果它們的bef[]!=now,則都更新一遍。直到有個節點p滿足bef[p]==now,那么就不需要再向上更新了(再往上已經更新過了)。(這個在insert后用np更新就可以啊)
這個暴力跳的復雜度可能是\(O(n\sqrt n)\)的,但是很難卡滿(廣義SAM上一個點暴力跳fa的次數是\(O(\sqrt n)\)的,具體見這里)。
有一種離線+DFS序+樹狀數組的做法可以做到\(\log n\)。(注意廣義SAM應該要像這題那么建)
但事實上這題還有個剪枝(bef[np]==now
),廣義SAM上情況比較復雜我也不知道真正的復雜度是啥。。
注意新建nq時 bef[nq],cnt[nq]也要復制(=...[q])。
//24612kb 76ms
#include <cstdio>
#include <cstring>
#include <algorithm>
#define gc() getchar()
const int N=2e5+5;struct Suffix_Automaton
{int las,tot,fa[N],son[N][26],len[N],cnt[N],bef[N];char s[360005];void Init(){las=tot=1;}void Insert(int now,int c){int p=las,np=++tot; len[las=np]=len[p]+1;for(; p&&!son[p][c]; p=fa[p]) son[p][c]=np;if(!p) fa[np]=1;else{int q=son[p][c];if(len[q]==len[p]+1) fa[np]=q;else{int nq=++tot;len[nq]=len[p]+1, bef[nq]=bef[q], cnt[nq]=cnt[q];//!memcpy(son[nq],son[q],sizeof son[q]);fa[nq]=fa[q], fa[q]=fa[np]=nq;for(; son[p][c]==q; p=fa[p]) son[p][c]=nq;}}for(; bef[np]!=now&&np; np=fa[np])++cnt[np], bef[np]=now;}void Build(int now){las=1, scanf("%s",s);for(int i=0,l=strlen(s); i<l; ++i)Insert(now,s[i]-'a');}void Query(){int p=1; scanf("%s",s);for(int i=0,l=strlen(s); i<l&&p; ++i)p=son[p][s[i]-'a'];printf("%d\n",cnt[p]);}
}sam;int main()
{int n,Q; scanf("%d%d",&n,&Q); sam.Init();for(int i=1; i<=n; ++i) sam.Build(i);while(Q--) sam.Query();return 0;
}