HDU - 4416 Good Article Good sentence(广义后缀自动机/后缀自动机)
题目链接:点击查看
题目大意:给出一个字符串 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++;}elsepos=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; }
总结
以上是生活随笔为你收集整理的HDU - 4416 Good Article Good sentence(广义后缀自动机/后缀自动机)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: HDU - 5769 Substring
- 下一篇: CodeForces - 1303D F