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 ...
随机推荐
- Linux 系统监控.诊断工具之 IO wait
1. 常用组合方式有如下几种: 用vmstat.sar.iostat检测是否是CPU瓶颈 用free.vmstat检测是否是内存瓶颈 用iostat.dmesg 检测是否是磁盘I/O瓶颈 用netst ...
- Jquery源码分析-整体结构
最近在学习Jquery的最新的源码,Jquery-3.3.1版本.网上有很多对jquery解析的文章.但是我还是要自己去尝试着看一篇jquery的源码.本系列博客用来记录其中的过程,并同大家分享.本次 ...
- iOS开发中的单元测试(三)——URLManager中的测试用例解析
本文转载至 http://www.cocoachina.com/cms/plus/view.php?aid=8088 此前,我们在<iOS开发中的单元测试(一)&(二)>中介绍 ...
- 使用MSSQL同步&发布数据库快照遇到错误:对路径“xxxxx”访问被拒绝的解决方法
使用MSSQL同步 数据库同步做后后测试:先在同步那台服务器(服务器A)数据库里修改里面数据库,然后再去被同步那台服务器(服务器B)看下数据有没被同步过去 发布数据库快照遇到错误:对路径“xxxxx” ...
- vmware虚拟机centos网络配置错误,执行/etc/init.d/network start 或 restart 提示Device eth0 has different MAC address than expected, ignoring
vmware虚拟机centos网络配置错误,执行/etc/init.d/network start 或 restart 提示Device eth0 has different MAC address ...
- Android笔记之启动界面的设置
默认情况下,启动界面是白屏 我们自定义一个启动界面如下,3秒钟后进入主界面并结束启动页 SplashActivity.java package com.bu_ish.myapp; import and ...
- 使用Scapy回放报文pcap
一.准备环境: Ubuntu + python2.7 sudo apt-get install python-scapy 二.准备报文: 先抓取一些报文,本实验使用的是DHCP的报文. 文件-导出 ...
- LightOJ1213 Fantasy of a Summation —— 快速幂
题目链接:https://vjudge.net/problem/LightOJ-1213 1213 - Fantasy of a Summation PDF (English) Statisti ...
- python 解析配置文件
settings.cfg [english] greeting = Hello [french] greeting = Bonjour [files] home = /usr/local bin = ...
- MYSQL进阶学习笔记十五:MySQL 的账号权限赋予!(视频序号:进阶_33,34)
知识点十六:MySQL的账号权限赋予(33) 一.MySQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你全力以内的事情,不可以越界.比如只允许你执行select操作,那么你就不能执 ...