HDU - 3065 病毒侵袭持续中(AC自动机)
题目链接:点击查看
题目大意:给出 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自动机)相关推荐
- HDU 3065病毒侵袭持续中 AC自动机
题意很明了,就是找每个匹配串在文本中出现的次数,并且根据题意可以可以有重复部分. 所以这个题与板子不同的地方就是查找的一部分. 还有就是多组输入!!! /*┆ ┏┓ ┏┓ ┆┆┏┛┻━━━━━━┛┻┓ ...
- hdu 3065 病毒侵袭持续中(AC自动机)
病毒侵袭持续中 Time Limit: 2000/1000 MS ...
- HDU-3065 病毒侵袭持续中 AC自动机又是一板子!
病毒侵袭持续中 上一题是求出现多少病毒输出病毒序号,而这题输出每个病毒出现的次数.这题有字典树基础都能做出来,把叶子节点用相应的编号标记起来,匹配的时候遍历到叶子节点用一个数组把次数存起来就行了. 有 ...
- HDU 3065 病毒侵袭持续中(AC自动机)题解
题意:要你找到主串中每个模式串的个数. 思路:题目都没说是多组数据,结果没while(~)直接WA了,和上一题差不多,可以用map或者开个数组储存.指针要记得回收内存,不然MLE. #include& ...
- BNUOJ 7178 病毒侵袭持续中
病毒侵袭持续中 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 30 ...
- 病毒侵袭持续中(HDU-3065)
Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒网站,他有 ...
- hdu 2896 病毒侵袭(AC自动机)
病毒侵袭 Time Limit: 2000/1000 ...
- HDU 2896 病毒侵袭 AC自动机
我表示不是很懂HDU卡内存的优良传统.......以及他们卡输出的良好风尚........ AC自动机裸体关键在于http://ascii.911cha.com/ #include<cstrin ...
- HDU 2896 病毒侵袭(AC自动机/模板题)
传送门 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
最新文章
- matlab直方图绘制
- OpenCV-Python 中文教程(搬运)目录
- java aspose重叠_Aspose.Words - 在特定位置合并两个文档
- mysql timeout
- 加密软件漏洞评测系统_惠州上线软件产品登记测试企业
- 安全运维 - Linux系统攻击回溯
- SoapUI使用教程
- Android个人日记本开发背景,毕业设计(论文)-手机app移动开发论文个人心情日记本的设计实现 .doc...
- 单片机引脚与端口,引脚和GPIO的区别
- Java 数据填充到word模板中
- Vue教程(三十九)vue-cli2脚手架安装
- 微信小程序自定义状态栏组件,提取自(colorui)
- c 语言 数字字符统计
- 《护理教育学》名词解释、简答题、问答题汇总
- C#制作一个简易抽奖或摇号器
- 过拟合及常见处理办法整理
- summernote支持视频上传功能,综合几个版本亲测可用
- by max over partition_Oracle高级查询之over(partition by...)
- 考研如何克服严重的拖延症?
- 大于在python中怎么打出来_在python中,如何检查输入不是空的并且是大于零的数字...
热门文章
- phpstudy mysql优化_MySQL_MySQL优化之对RAND()的优化方法,众所周知,在MySQL中,如果直 - phpStudy...
- MySQL高级 - 查询缓存 - SELECT选项
- SpringSecurity 认证通过后显示当前认证用户名
- 为什么需要Survivor区?只有Eden不行吗?
- 重量级锁的加锁的基本流程
- 由一个问题引发的思考
- 【问题2】为什么TIME_WAIT状态需要经过2MSL(最大报文段生存时间)才能返回到CLOSE状态?
- 用户退出登录清空cookie
- SpringBoot_数据访问-JDBC自动配置原理
- 模拟实现顺序表ArrayList2(三级)