题目链接:点击查看

题目大意:给出 n 个模式串和一个文本串,问每个模式串在文本串中分别出现了多少次

题目分析:虽然暴跳fail也是可以实现这个题目的,但个人感觉更好的方法还是建立fail树后在树上dfs比较好,需要注意的就是MLE的问题,因为这个题目是多组输入,直接memset会卡内存,所以不妨自己写一个newnode函数,用多少初始化多少,比较方便

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;char s[1010][60],str[2000100];int fail[N],trie[N][130],rk[N],dp[N],cnt;struct Edge
{int to,next;
}edge[N];int head[N],tot;void addedge(int u,int v)
{edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;
}int newnode()
{cnt++;for(int i=0;i<130;i++)trie[cnt][i]=0;return cnt;
}void insert_word(int id)
{int len=strlen(s[id]);int pos=0;for(int i=0;i<len;i++){int to=s[id][i];if(!trie[pos][to])trie[pos][to]=newnode();pos=trie[pos][to];}rk[id]=pos;
}void getfail()
{queue<int>q;for(int i=0;i<130;i++){if(trie[0][i]){fail[trie[0][i]]=0;q.push(trie[0][i]);}}while(!q.empty()){int cur=q.front();q.pop();for(int i=0;i<130;i++){if(trie[cur][i]){fail[trie[cur][i]]=trie[fail[cur]][i];q.push(trie[cur][i]);}elsetrie[cur][i]=trie[fail[cur]][i];}}
}int search_word()
{int len=strlen(str),pos=0;for(int i=0;i<len;i++){int to=str[i];pos=trie[pos][to];dp[pos]++;}
}void dfs(int u)
{for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;dfs(v);dp[u]+=dp[v];}
}void init()
{memset(head,-1,sizeof(head));memset(dp,0,sizeof(dp));for(int i=0;i<130;i++)trie[0][i]=0;cnt=tot=0;
}int main()
{
//  freopen("input.txt","r",stdin);
//  ios::sync_with_stdio(false);int n;while(scanf("%d",&n)!=EOF){init();for(int i=1;i<=n;i++){scanf("%s",s[i]);insert_word(i);}getfail();scanf("%s",str);search_word();for(int i=1;i<=cnt;i++)addedge(fail[i],i);dfs(0);for(int i=1;i<=n;i++)if(dp[rk[i]])printf("%s: %d\n",s[i],dp[rk[i]]);}return 0;
}

HDU - 3065 病毒侵袭持续中(AC自动机)相关推荐

  1. HDU 3065病毒侵袭持续中 AC自动机

    题意很明了,就是找每个匹配串在文本中出现的次数,并且根据题意可以可以有重复部分. 所以这个题与板子不同的地方就是查找的一部分. 还有就是多组输入!!! /*┆ ┏┓ ┏┓ ┆┆┏┛┻━━━━━━┛┻┓ ...

  2. hdu 3065 病毒侵袭持续中(AC自动机)

    病毒侵袭持续中                                                                     Time Limit: 2000/1000 MS ...

  3. HDU-3065 病毒侵袭持续中 AC自动机又是一板子!

    病毒侵袭持续中 上一题是求出现多少病毒输出病毒序号,而这题输出每个病毒出现的次数.这题有字典树基础都能做出来,把叶子节点用相应的编号标记起来,匹配的时候遍历到叶子节点用一个数组把次数存起来就行了. 有 ...

  4. HDU 3065 病毒侵袭持续中(AC自动机)题解

    题意:要你找到主串中每个模式串的个数. 思路:题目都没说是多组数据,结果没while(~)直接WA了,和上一题差不多,可以用map或者开个数组储存.指针要记得回收内存,不然MLE. #include& ...

  5. BNUOJ 7178 病毒侵袭持续中

    病毒侵袭持续中 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 30 ...

  6. 病毒侵袭持续中(HDU-3065)

    Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒网站,他有 ...

  7. hdu 2896 病毒侵袭(AC自动机)

    病毒侵袭                                                                           Time Limit: 2000/1000 ...

  8. HDU 2896 病毒侵袭 AC自动机

    我表示不是很懂HDU卡内存的优良传统.......以及他们卡输出的良好风尚........ AC自动机裸体关键在于http://ascii.911cha.com/ #include<cstrin ...

  9. HDU 2896 病毒侵袭(AC自动机/模板题)

    传送门 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)            Memory Limit: 131072/131072 K (Java/Other ...

最新文章

  1. matlab直方图绘制
  2. OpenCV-Python 中文教程(搬运)目录
  3. java aspose重叠_Aspose.Words - 在特定位置合并两个文档
  4. mysql timeout
  5. 加密软件漏洞评测系统_惠州上线软件产品登记测试企业
  6. 安全运维 - Linux系统攻击回溯
  7. SoapUI使用教程
  8. Android个人日记本开发背景,毕业设计(论文)-手机app移动开发论文个人心情日记本的设计实现 .doc...
  9. 单片机引脚与端口,引脚和GPIO的区别
  10. Java 数据填充到word模板中
  11. Vue教程(三十九)vue-cli2脚手架安装
  12. 微信小程序自定义状态栏组件,提取自(colorui)
  13. c 语言 数字字符统计
  14. 《护理教育学》名词解释、简答题、问答题汇总
  15. C#制作一个简易抽奖或摇号器
  16. 过拟合及常见处理办法整理
  17. summernote支持视频上传功能,综合几个版本亲测可用
  18. by max over partition_Oracle高级查询之over(partition by...)
  19. 考研如何克服严重的拖延症?
  20. 大于在python中怎么打出来_在python中,如何检查输入不是空的并且是大于零的数字...

热门文章

  1. phpstudy mysql优化_MySQL_MySQL优化之对RAND()的优化方法,众所周知,在MySQL中,如果直 - phpStudy...
  2. MySQL高级 - 查询缓存 - SELECT选项
  3. SpringSecurity 认证通过后显示当前认证用户名
  4. 为什么需要Survivor区?只有Eden不行吗?
  5. 重量级锁的加锁的基本流程
  6. 由一个问题引发的思考
  7. 【问题2】为什么TIME_WAIT状态需要经过2MSL(最大报文段生存时间)才能返回到CLOSE状态?
  8. 用户退出登录清空cookie
  9. SpringBoot_数据访问-JDBC自动配置原理
  10. 模拟实现顺序表ArrayList2(三级)