题目链接:点击查看

题目大意:给出一个字符串 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(广义后缀自动机/后缀自动机)相关推荐

  1. HDU 4416 Good Article Good sentence(12年杭州 后缀数组)

    转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526       by---cxlove 题目:给出一个A串,给出若干个B ...

  2. HDU 4416 Good Article Good sentence(后缀数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4416 题意:给出一个串A和多个串B.求A有多少不重复的子串使得这些子串不是B中任意一个串的子串? 思路 ...

  3. HDU 4416 (后缀自动机)

    HDU 4416 Good Article Good sentence Problem : 给一个串S,和一些串T,询问S中有多少个子串没有在T中出现. Solution :首先对所有的T串建立后缀自 ...

  4. HDU 3518 HDU 4416【后缀自动机len的使用】

    max:即代码中 len 变量,它表示该状态能够接受的最长的字符串长度. min:表示该状态能够接受的最短的字符串长度.实际上等于该状态的 fail 指针指向的结点的 len + 1. max-min ...

  5. 使用栈解决的一类经典问题:表达式转换及求值;中缀表达式;前缀表达式,后缀表达式,中缀转前缀;中缀转后缀;后缀表达式求值;波兰式,逆波兰式

    文章目录 背景知识 表达式转换问题(考研经典) 一:手工转换 (1)中缀转前缀和中缀转后缀 (2)前缀转中缀和后缀转中缀 二:用栈实现表达式转换 (1)中缀转后缀 (2)中缀转前缀 表达式计算问题(使 ...

  6. 后缀自动机 AC自动机

    trie树可遍历出所有无重复后缀,通过后缀遍历前缀可得到所有子串:后缀链接把所有后缀相同的状态(以当前节点为endpos的子串)连接起来,便有了类似KMP的next数组的性质.             ...

  7. 表达式求值(中缀转后缀及后缀表达式求值)

    .中缀表达式转后缀表达式: 中缀表达式转后缀表达式遵循以下原则: 1.遇到操作数,直接输出: 2.栈为空时,遇到运算符,入栈: 3.遇到左括号,将其入栈: 4.遇到右括号,执行出栈操作,并将出栈的元素 ...

  8. 中缀转后缀,后缀表达式求值

    中缀表达式转后缀表达式步骤: 1.初始化一个栈,用于保存 暂时还不能确定运算顺序的运算符 2.扫描各个元素2.1遇到操作数,直接加入后缀表达式2.2遇到界限符2.2.1 遇到"(" ...

  9. 【HDU4416】Good Article Good sentence【后缀数组】

    研究了一会才搞懂. 参照blog(http://blog.csdn.net/acm_cxlove/article/details/7854526 by---cxlove)写的代码.. 先把A和所有的B ...

最新文章

  1. usaco Prime Cryptarithm
  2. Kotlin学习笔记-基础语法
  3. exchange server 2003 错误处理
  4. [转]COM线程模型-套间
  5. PyCharm安装Twisted库(报错:Microsoft Visual C++ 14.0 is required. Get it with “Build Tools for Visual Stu)
  6. python中的urllib库_python3里的Urllib库
  7. 从入门到入土:python爬虫|scrapy初体验|安装教程|爬取豆瓣电影短评相关信息(昵称,内容,时间和评分)
  8. MS Sql当中 money类型数据使用 Decimal 传输
  9. SPSS数据分析全套教程(1)——SPSS概览
  10. MFC CImageList序列图的用法
  11. 2022自编译最新稳定版newifi3固件
  12. 观远数据带你乘云驾“务”,让决策更智能
  13. 关闭惠普计算机通电启动注册表,惠普电脑总是自动重启如何解决
  14. 给女朋友讲解什么是代理模式
  15. 时间序列分类01:人类活动识别深度学习模型综述
  16. 数值分析复化求积matlab,MATLAB数值分析实验二(复合梯形、辛普森和龙贝格求积,以及二重积分计算等)...
  17. ubuntu 14.04 成功迁移根分区到SSD
  18. python字体类型arial_python-3.x - 为什么我的font.name属性不影响使用Python-pptx制作的ppt上的字体? 我总是得到arial字体 - 堆栈内存溢出...
  19. matlab短时过零率计算+源代码
  20. 运行sfc/scannow的时候弹出插入cd对话框

热门文章

  1. matlab均线程序化交易,【策略分享】Matlab量化交易策略源码分享
  2. Nacos源码HostReactor
  3. Nacos-Nacos和Eureka的对比
  4. Nginx负载均衡策略之ip_hash
  5. 幻读(phantom read)
  6. AOP 中必须明白的概念-通知(Advice)
  7. MyBatis的插入数据操作
  8. HashSet存储自定义类型元素
  9. 字符输入流_Reader类FileReader类介绍
  10. Lambda省略格式Lambda使用前提