HDU2896【AC自动机-模板】
思路:
因为不同病毒特征码不会相同。
AC自动机,然后对于每一个输出即可。
注意:以上字符串中字符都是ASCII码可见字符(不包括回车);G++ MLE。
//#include <bits/stdc++.h>
#include<iostream>
#include<queue>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std; const int N=1e5+10; //500个串,长度为200 struct Trie{
int num;
Trie *next[128],*fail;
};
Trie q[N],*root;
int tol; Trie* Creat()
{
Trie *p;
p=&q[tol++];
p->fail=NULL;
p->num=0;
for(int i=0;i<128;i++)
p->next[i]=NULL;
return p;
} void Insert(char *str,int num)
{
int len=strlen(str),index;
Trie *p=root;
for(int i=0;i<len;i++)
{
index=str[i];
if(p->next[index]==NULL)
p->next[index]=Creat();
p=p->next[index];
}
p->num=num;
} void Build_Ac()
{
queue<Trie*>que;
que.push(root);
while(!que.empty())
{
Trie *p=que.front();que.pop();
for(int i=0;i<128;i++)
{
if(p->next[i]!=NULL)
{
if(p==root) p->next[i]->fail=root;
else{
Trie *temp=p->fail;
while(temp!=NULL)
{
if(temp->next[i]!=NULL) {
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL)
p->next[i]->fail=root;
}
que.push(p->next[i]);
}
}
}
} int ans[510],nn;
char word[10010];
void Query()
{
Trie *p=root;
int index,len=strlen(word);
for(int i=0;i<len;i++)
{
index=word[i];
while(p!=root && p->next[index]==NULL)
p=p->fail;
p=p->next[index];
if(p==NULL)
p=root;
Trie *temp=p;
while(temp!=root)
{
if(temp->num)
ans[nn++]=temp->num;
temp=temp->fail;
}
}
} char s[220];
int main()
{
tol=0;
root=Creat();
int n,m;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
Insert(s,i);
}
Build_Ac();
scanf("%d",&m);
int sum=0;
for(int i=1;i<=m;i++)
{
scanf("%s",word);
nn=0;
Query();
if(nn)
{
sum++;
sort(ans,ans+nn);
printf("web %d:",i);
for(int i=0;i<nn;i++)
printf(" %d",ans[i]);
puts("");
}
}
printf("total: %d\n",sum);
return 0;
}
HDU2896【AC自动机-模板】的更多相关文章
- HDu-2896 病毒侵袭,AC自动机模板题!
病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU 2222 (AC自动机模板题)
题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...
- HDU2896+AC自动机
ac自动机 模板题 /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include&l ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- KMP与AC自动机模板
HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...
随机推荐
- 04 http协议模拟登陆发帖
<?php require('./http.class.php'); $http = new Http('http://home.verycd.com/cp.php?ac=pm&op=s ...
- EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器
在不断进行EasyDarwin开源流媒体服务器的功能和性能完善的同时,我们也配套实现了目前在安防和移动互联网行业比较火热的移动端手机直播方案,主要就是我们的 EasyPusher直播推送项目 和 Ea ...
- PO 审批及生成xml文件
*********************************************************************** * Report : YTST_RAINY_MM2 * ...
- jQuery事件函数位置放置的两种方法
jQuery 事件函数 jQuery 事件处理方法是 jQuery 中的核心函数. 事件处理程序指的是当 HTML 中发生某些事件时所调用的方法. 通常会把 jQuery 代码放到 <head& ...
- shapes
接口 shape package shape; public abstract interface shape { public abstract void Draw(); public abstra ...
- Linux系统中10个常用的ps命令总结
Linux作为Unix的衍生操作系统,Linux内建有查看当前进程的工具ps.这个工具能在命令行中使用PS 命令是什么 查看它的man手册可以看到,ps命令能够给出当前系统中进程的快照.它能捕获系统在 ...
- 基于logstash+elasticsearch+kibana的日志收集分析方案(Windows)
一 方案背景 通常,日志被分散的储存不同的设备上.如果你管理数十上百台服务器,你还在使用依次登录每台机器的传统方法查阅日志.这样是不是感觉很繁琐和效率低下.开源实时日志分析ELK平台能够完美的 ...
- RTMP协议的理解
RTMP协议:real time message protocol 工作原理: 先采集摄像头视频和麦克风音频信息,再进行音视频的编码(mpeg),通过FMLE(Flash Media Live Enc ...
- kali本機安裝openvas的血淚史復盤
安裝openvas的血淚史 因爲學習的需要,需要裝openvas,但是在虛擬機裏面,無論怎麼更新跟新源,總是會有問題,一氣之下,便不用虛擬機了,將自己的物理機刷成了kali機,從此便進了一個大坑. 安 ...
- configure: error: APR not found. Please read the documentation
本以为Apache的编译安装很简单,其实不然: 以前的环境下编译报错很少 ,但这次不行了 提示configure: error: APR not found. Please read the docu ...