病毒侵袭 HDU - 2896(ac自动机 板题)
但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小t不幸成为受害者之一。小t如此生气,他决定要把世界上所有带病毒的网站都找出来。当然,谁都知道这是不可能的。小t却执意要完成这不能的任务,他说:“子子孙孙无穷匮也!”(愚公后继有人了)。
万事开头难,小t收集了好多病毒的特征码,又收集了一批诡异网站的源码,他想知道这些网站中哪些是有病毒的,又是带了怎样的病毒呢?顺便还想知道他到底收集了多少带病毒的网站。这时候他却不知道何从下手了。所以想请大家帮帮忙。小t又是个急性子哦,所以解决问题越快越好哦~~
Input第一行,一个整数N(1<=N<=500),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在20—200之间。
每个病毒都有一个编号,依此为1—N。
不同编号的病毒特征码不会相同。
在这之后一行,有一个整数M(1<=M<=1000),表示网站数。
接下来M行,每行表示一个网站源码,源码字符串长度在7000—10000之间。
每个网站都有一个编号,依此为1—M。
以上字符串中字符都是ASCII码可见字符(不包括回车)。
Output依次按如下格式输出按网站编号从小到大输出,带病毒的网站编号和包含病毒编号,每行一个含毒网站信息。
web 网站编号: 病毒编号 病毒编号 …
冒号后有一个空格,病毒编号按从小到大排列,两个病毒编号之间用一个空格隔开,如果一个网站包含病毒,病毒数不会超过3个。
最后一行输出统计信息,如下格式
total: 带病毒网站数
冒号后有一个空格。
Sample Input
3
aaa
bbb
ccc
2
aaabbbccc
bbaacc
Sample Output
web 1: 1 2 3
total: 1
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int tot, id;
queue<int> q;
vector<int> v; struct node
{
int next[];
int fail, cnt;
}trie[maxn]; void init()
{
while(!q.empty()) q.pop();
for(int i=; i<maxn; i++)
{
mem(trie[i].next, );
trie[i].cnt = trie[i].fail = ;
}
tot = id = ;
} void insert(char *s)
{
int n = strlen(s);
int rt = ;
for(int i=; i<n; i++)
{
int x = s[i];
if(trie[rt].next[x] == ) trie[rt].next[x] = ++tot;
rt = trie[rt].next[x];
}
trie[rt].cnt = ++id;
} void build()
{
trie[].fail = -;
q.push();
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i=; i<; i++)
{
if(trie[u].next[i])
{
if(u == ) trie[trie[u].next[i]].fail = ;
else
{
int v = trie[u].fail;
while(v != -)
{
if(trie[v].next[i])
{
trie[trie[u].next[i]].fail = trie[v].next[i];
break;
}
v = trie[v].fail;
}
if(v == -) trie[trie[u].next[i]].fail = ;
}
q.push(trie[u].next[i]);
}
}
}
} void Get(int u)
{
while(u)
{
if(trie[u].cnt)
v.push_back(trie[u].cnt);
u = trie[u].fail;
}
} bool match(char *s, int k)
{
int n = strlen(s);
int rt = ;
for(int i=; i<n; i++)
{
int x = s[i];
if(trie[rt].next[x])
rt = trie[rt].next[x];
else
{
int p = trie[rt].fail;
while(p != - && trie[p].next[x] == ) p = trie[p].fail;
if(p == -) rt = ;
else rt = trie[p].next[x];
}
// if(trie[rt].cnt)
Get(rt);
}
if(!v.empty())
{
sort(v.begin(), v.end());
// for(int i=0; i<v.size(); i++)
// cout<< v[i] <<endl;
v.erase(unique(v.begin(), v.end()), v.end());
printf("web %d:", k);
for(int i=; i<v.size(); i++)
printf(" %d", v[i]);
printf("\n");
return true;
}
return false;
} int n, m, res = ;
char s[maxn]; int main()
{
scanf("%d", &n);
rap(i, , n)
{
scanf("%s", s);
insert(s);
}
build();
scanf("%d", &m);
rap(i, , m)
{
v.clear();
scanf("%s", s);
if(match(s, i))
res++;
}
printf("total: %d\n", res); return ;
}
病毒侵袭 HDU - 2896(ac自动机 板题)的更多相关文章
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。
/** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...
- hdu 2896 AC自动机模版题
题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).” -----也就说AC自动机的Trie树需要128个单词分支. ...
- HDU 2896 AC自动机 裸题
中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...
- hdu 2896 AC自动机
// hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- [hdu3065]病毒侵袭持续中(AC自动机)
题意:给出多种病毒的号码和特征码,计算在某串中各病毒匹配的次数. 解题关键:AC自动机模板题,多组输入坑人. #include<bits/stdc++.h> using namespace ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 病毒侵袭 HDU - 2896 板子题
当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~ 但网路上总有那么些网站,开 ...
随机推荐
- CF 1025 D. Recovering BST
D. Recovering BST http://codeforces.com/contest/1025/problem/D 题意: 给出一个连续上升的序列a,两个点之间有边满足gcd(ai ,aj) ...
- SRM 653 CountryGroupHard
SRM 653 CountryGroupHard 题意: n 个人坐成一排,同一个国家的人坐在一起,记者问了一些人他们的国家有多少人,他们都回答了正确的信息.已知m条信息,每条信息形如第 pi 个位置 ...
- PHP 中call_user_func相关函数的使用
call_user_func 官方的解释是:把第一个参数作为回调函数(callback),并且将其余的参数作为回调函数的参数. 第一个参数可以是函数名,后面的均为作为该函数使用的参数. 1. call ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 【转】AOE机制的DSL及其实际运用
AOE这个词的意思,我相信玩过WOW的人都不陌生,包括玩过LoL的也不会陌生,说穿了就是一个区域内发生效果(Area of effect).这里我们要讨论的就是关于一个适合于几乎所有游戏的AOE机制, ...
- 3星|《CMO到底能干多久?》:CEO必须决定供公司需要哪类CMO
CMO到底能干多久?(<哈佛商业评论>增刊) <哈佛商业评论>关于CMO的6篇文章.我认为第一篇<为何CMO宝座坐不长>写的最好,是典型的哈评风格:做过实际调查.有 ...
- 论文笔记:Fully-Convolutional Siamese Networks for Object Tracking
Fully-Convolutional Siamese Networks for Object Tracking 本文作者提出一个全卷积Siamese跟踪网络,该网络有两个分支,一个是上一帧的目标,一 ...
- 【RL系列】马尔可夫决策过程——Jack‘s Car Rental
本篇请结合课本Reinforcement Learning: An Introduction学习 Jack's Car Rental是一个经典的应用马尔可夫决策过程的问题,翻译过来,我们就直接叫它“租 ...
- DevOps之六 shell以及pipeline 命令部署
一 使用shell命启动spring boot 项目 1. 使用shell停止当前项目 #!/bin/sh main() { clear pid=`ps -ef|grep xx.jar|grep -v ...
- 用VS测试程序
怀着一种忐忑的心情,我开始了我的软件测试. #include "stdio.h" #include "stdlib.h" int main(int argc, ...