题目链接: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自动机(病毒侵袭 )的更多相关文章

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

    HDU 3065 题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/C Description 小t ...

  2. AC自动机---病毒侵袭

    HDU 2896 题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/B Description 当太 ...

  3. AC日记——病毒侵袭持续中 hdu 3065

    3065 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...

  4. AC日记——病毒侵袭 hdu 2896

    2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...

  5. 【HDU2896】病毒侵袭 AC自动机

    [HDU2896]病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年 ...

  6. hdu 2896 病毒侵袭 ac自动机

    /* hdu 2896 病毒侵袭 ac自动机 从题意得知,模式串中没有重复的串出现,所以结构体中可以将last[](后缀链接)数组去掉 last[]数组主要是记录具有相同后缀模式串的末尾节点编号 .本 ...

  7. hdu2896 病毒侵袭 ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2896 题目: 病毒侵袭 Time Limit: 2000/1000 MS (Java/Othe ...

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

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. 【HDU3065】 病毒侵袭持续中(AC自动机)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  10. hdu 2896 病毒侵袭 AC自动机(查找包含哪些子串)

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. sass和less的几点不同

    1.申明和使用变量 sass使用$符号来标识变量(老版本的sass使用!来标识变量),比如$highlight-color和$sidebar-width. 与CSS属性不同,变量可以在css规则块定义 ...

  2. Notes of Daily Scrum Meeting(12.21)

    今天的燃尽图把周六的进度加了进来,由于我的失误没有及时更新TFS,所以出现了一些错误,向大家道歉. 下面是今天的任务总结: 团队成员 今日团队工作 陈少杰 继续进行网络连接的调试 王迪 测试搜索的功能 ...

  3. 《Linux内核设计与实现》第五章读书笔记

    第五章  系统调用 5.1与内核通信 1. 系统调用 让应用程序受限的访问硬件设备 提供创建新进程并与已有进程通信的机制 提供申请操作系统其他资源能力是用户空间进程和硬件设备之间的中间层 2. 系统调 ...

  4. Sprint report

    Sprint report 一.需求分析:随着在校大学生人数的不断增加,许多高校出现了许多个校区并存的局面,并且校区之间的地理位置跨度非常大,给高校选课带来了很大的不方便,数据处理手工操作,工作量大, ...

  5. [转帖] wordpress 的安装过程

    https://blog.csdn.net/qq_34364668/article/details/78116473 下载Wordpress wget http://wordpress.org/lat ...

  6. [转帖]VMware Vsphere 6.0安装部署 (三) vCenter Server安装

    VMware Vsphere 6.0安装部署 (三) vCenter Server安装 2016年08月29日 14:59:14 dAng1r0Us 阅读数:72942   版权声明:本文为博主原创文 ...

  7. php 有意思的小题

    /** * 你的是一个数字和一个字符串进行比较,PHP会把字符串转换成数字再进行比较.* PHP转换的规则的是:若字符串以数字开头,则取开头数字作为转换结果,若无则输出0.***/1)$a =”abc ...

  8. webpack 搭建vue项目流程

    1.安装node 2.打开命令行输入  npm install -g vue-cli 3.vue init webpack-simple vue 4.各种确认(enter键) 5.npm instal ...

  9. Python中xlrd和xlwt模块读写Excel的方法

    本文主要介绍可操作excel文件的xlrd.xlwt模块.其中xlrd模块实现对excel文件内容读取,xlwt模块实现对excel文件的写入. 着重掌握读取操作,因为实际工作中读取excel用得比较 ...

  10. 【版本管理】git远程管理

    GitHub相关:       第1步:注册github账号,创建SSH Key. 在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件, ...