AC自动机(病毒侵袭 )
题目链接:https://cn.vjudge.net/contest/280743#problem/B
题目大意:中文题目
具体思路:AC自动机模板题,编号的时候注意,是按照给定的id进行编号的。然后输出的时候注意去重,虽然按道理来讲通过last数组是不会有重复的,但是如果是这种情况,病毒模板aaa,然后给你一个模板串aaaaa,这样的话,就肯定会有重复的了,所以需要去重,其次输出的时候按照升序输出。
AC代码:
#include<iostream>
#include<stack>
#include<stdio.h>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
# define ll long long
const int maxn = 2e5+;
const int maxm = 1e6+;
char str1[+],str2[+];
int tot,ch[maxn][],val[maxn];
int fail[maxn],last[maxn],sto[+],vis[+];
int ans;
void add(int t)
{
int p=;
int len=strlen(str1);
for(int i=; i<len; i++)
{
int u=(int)str1[i];
if(!ch[p][u])
ch[p][u]=++tot;
p=ch[p][u];
}
val[p]=t;
}
void getfail()
{
queue<int>q;
for(int i=; i<; i++)
{
if(ch[][i])
q.push(ch[][i]);
}
while(!q.empty())
{
int top=q.front();
q.pop();
for(int i=; i<; i++)
{
int u=ch[top][i];
if(u==)
continue;
q.push(u);
int v=fail[top];
while(v&&ch[v][i]==)
v=fail[v];
fail[u]=ch[v][i];
last[u]=val[fail[u]] ? fail[u] : last[fail[u]];
}
}
}
void cal(int t)
{
while(t)
{
if(val[t]&&vis[val[t]]==)//去重的过程。
sto[++ans]=val[t],vis[val[t]]=;
// val[t]=0;// 注意这个地方,不能改成0,因为不只有一个字符串需要去匹配。
t=last[t];
}
}
void getans()
{
int len=strlen(str2);
int p=;
for(int i=; i<len; i++)
{
int u=(int)str2[i];
while(p&&ch[p][u]==)
p=fail[p];
p=ch[p][u];
if(val[p])
cal(p);
else if(fail[p])
cal(fail[p]);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=; i<=n; i++)
{
scanf("%s",str1);
add(i);
}
getfail();
int m;
scanf("%d",&m);
int num=;
for(int i=; i<=m; i++)
{
memset(vis,,sizeof(vis));
ans=;
scanf("%s",str2);
getans();
if(ans==)
continue;
num++;
printf("web %d:",i);
sort(sto+,sto+ans+);//按照升序输出。
for(int j=; j<=ans; j++)
{
printf(" %d",sto[j]);
}
printf("\n");
}
printf("total: %d\n",num);
return ;
}
AC自动机(病毒侵袭 )的更多相关文章
- AC自动机---病毒侵袭持续中
HDU 3065 题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/C Description 小t ...
- AC自动机---病毒侵袭
HDU 2896 题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/B Description 当太 ...
- AC日记——病毒侵袭持续中 hdu 3065
3065 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...
- AC日记——病毒侵袭 hdu 2896
2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...
- 【HDU2896】病毒侵袭 AC自动机
[HDU2896]病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年 ...
- hdu 2896 病毒侵袭 ac自动机
/* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...
- hdu2896 病毒侵袭 ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2896 题目: 病毒侵袭 Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 2896 病毒侵袭(AC自动机)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 【HDU3065】 病毒侵袭持续中(AC自动机)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)
病毒侵袭 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- pdf修复
pdf工具下载地址: 链接:https://pan.baidu.com/s/1SgGSrH7apX64hQEl732wWg 提取码:kg5q 使用说明: 1.含注册命令,先注册再运行.
- 第一次冲刺阶段后五天总结和第一次 Sprint
第一次冲刺阶段后五天总结(11.16-11.20) 一.准备实现的功能 1 在游戏中随机显示式子 点击进入答题则进入答题界面,显示式子 2 设置限时或者规定答题数目模式 进入游戏,选择不同模式 3 产 ...
- Spring所需的Jar包下载
作者:zhidashang 来源:CSDN 原文:https://blog.csdn.net/zhidashang/article/details/78706027 版权声明:本文为博主原创文章,转载 ...
- Alpha冲刺——day10
Alpha冲刺--day10 作业链接 Alpha冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602634 ...
- PAT 甲级 1057 Stack
https://pintia.cn/problem-sets/994805342720868352/problems/994805417945710592 Stack is one of the mo ...
- [转帖]win10 .Net Runtime Optimization Service占用大量CPU资源解决方法
win10 .Net Runtime Optimization Service占用大量CPU资源解决方法 https://blog.csdn.net/cwg2552298/article/detail ...
- code review & github
code review & github code review https://github.com/features/code-review/ https://github.com/mar ...
- 异构数据库之间完全可以用SQL语句导数据
告诉你一个最快的方法,用SQLServer连接DBF 在SQLServer中执行 SELECT * into bmk FROM OpenDataSource( ‘Microsoft.Jet.OLEDB ...
- 普通的jdbc事务在插入数据后 下面的代码报错时 数据不会回滚 但是 spring的事务会回滚
普通的jdbc事务在插入数据后 下面的代码报错时 数据不会回滚 但是 spring的事务会回滚
- oracle 查出一个表中字段值出现次数大于2的所有记录
表web_order 列 name ,businesscode, a.account 周桥 18929609222 3754031157710000妙药 18929609233 3754031157 ...