xtu字符串 D. 病毒侵袭
D. 病毒侵袭
64-bit integer IO format: %I64d Java class name: Main
但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小t不幸成为受害者之一。小t如此生气,他决定要把世界上所有带病毒的网站都找出来。当然,谁都知道这是不可能的。小t却执意要完成这不能的任务,他说:“子子孙孙无穷匮也!”(愚公后继有人了)。
万事开头难,小t收集了好多病毒的特征码,又收集了一批诡异网站的源码,他想知道这些网站中哪些是有病毒的,又是带了怎样的病毒呢?顺便还想知道他到底收集了多少带病毒的网站。这时候他却不知道何从下手了。所以想请大家帮帮忙。小t又是个急性子哦,所以解决问题越快越好哦~~
Input
接下来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 解题:AC自动机的模板题。。。。哎。。。改了一天。。。。终于满意了。。。。。。。。。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <queue>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
struct trie {
int cnt,id,wd[],fail;
void init() {
id = cnt = ;
fail = -;
memset(wd,-,sizeof(wd));
}
} dic[maxn];
int tot,ans[],total;
void insertWord(int root,int _id,char *s) {
for(int i = ; s[i]; i++) {
int k = s[i] - ;
if(dic[root].wd[k] == -) {
dic[tot].init();
dic[root].wd[k] = tot++;
}
root = dic[root].wd[k];
}
dic[root].cnt++;
dic[root].id = _id;
}
void build(int root) {
queue<int>q;
q.push(root);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = ; i < ; i++) {
if(dic[u].wd[i] == -) continue;
if(!u) dic[dic[u].wd[i]].fail = ;//如果是第二层的节点
else {
int v = dic[u].fail;
while(v && dic[v].wd[i] == -)
v = dic[v].fail;
//回溯到离根较远并与当前字符相同的点
if(dic[v].wd[i] != -)
dic[dic[u].wd[i]].fail = dic[v].wd[i];
else dic[dic[u].wd[i]].fail = ;
}
q.push(dic[u].wd[i]);
}
}
} void query(int root,char *s) {
bool vis[] = {false};
for(int i = ; s[i]; i++) {
int k = s[i] - ;
while(root && dic[root].wd[k] == -)
root = dic[root].fail;//不如当前字符匹配,回溯
root = dic[root].wd[k];//dic[root].wd[k]与当前字符匹配
if(root == -) root = ;//trie树上不存在与之匹配的
else {
int v = root;
while(v && !vis[dic[v].id]) {
//如果当前节点访问过了,
//从当前节点的回溯路径上的节点也被访问了
if(dic[v].cnt) {
vis[dic[v].id] = true;
ans[total++] = dic[v].id;
}
v = dic[v].fail;
}
}
}
}
int main() {
int n,m,i,j,t = ;
char word[],text[];
scanf("%d",&n);
dic[].init();
tot = ;
for(i = ; i <= n; i++) {
scanf("%s",word);
insertWord(,i,word);
}
build();
scanf("%d",&m);
for(i = ; i <= m; i++) {
total = ;
scanf("%s",text);
query(,text);
if(total) {
t++;
sort(ans,ans+total);
printf("web %d:",i);
for(j = ; j < total; j++)
printf(" %d",ans[j]);
printf("\n");
}
}
printf("total: %d\n",t);
return ;
}
Trie图
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int ret;
struct Trie{
int ch[maxn][],fail[maxn],cnt[maxn],tot;
int newnode(){
memset(ch[tot],,sizeof ch[tot]);
fail[tot] = cnt[tot] = ;
return tot++;
}
void init(){
tot = ;
newnode();
}
void insert(char *str,int id,int root = ){
for(int i = ; str[i]; ++i){
if(!ch[root][str[i]-]) ch[root][str[i]-] = newnode();
root = ch[root][str[i]-];
}
cnt[root] = id;
}
void build(int root = ){
queue<int>q;
for(int i = ; i < ; ++i)
if(ch[root][i]) q.push(ch[root][i]);
while(!q.empty()){
root = q.front();
q.pop();
for(int i = ; i < ; ++i){
if(ch[root][i]){
fail[ch[root][i]] = ch[fail[root]][i];
q.push(ch[root][i]);
}else ch[root][i] = ch[fail[root]][i];
}
}
}
void query(char *str,int id,int root = ){
vector<int>ans;
bool vis[] = {false};
for(int i = ; str[i]; ++i){
int x = root = ch[root][str[i]-];
while(x && !vis[cnt[x]]){
if(cnt[x]) ans.push_back(cnt[x]);
vis[cnt[x]] = true;
x = fail[x];
}
}
if(ans.size()){
++ret;
sort(ans.begin(),ans.end());
printf("web %d:",id);
for(auto it:ans) printf(" %d",it);
putchar('\n');
}
}
}ac;
char str[maxn];
int main(){
int n,m;
while(~scanf("%d",&n)){
ac.init();
for(int i = ; i <= n; ++i){
scanf("%s",str);
ac.insert(str,i);
}
scanf("%d",&m);
ac.build();
ret = ;
for(int i = ; i <= m; ++i){
scanf("%s",str);
ac.query(str,i);
}
printf("total: %d\n",ret);
}
return ;
}
xtu字符串 D. 病毒侵袭的更多相关文章
- 【HDU2896】病毒侵袭 AC自动机
[HDU2896]病毒侵袭 Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年 ...
- hdu3065 病毒侵袭持续中
题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3065 题目: 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java ...
- 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 ...
- AC自动机---病毒侵袭持续中
HDU 3065 题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/C Description 小t ...
- HDU 3065 病毒侵袭持续中
HDU 3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu----(3065)病毒侵袭持续中(AC自动机)
病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hduoj-----(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( ...
随机推荐
- 数学 2015百度之星初赛2 HDOJ 5255 魔法因子
题目传送门 /* 数学:不会写,学习一下这种解题方式:) 思路:设符合条件的数的最高位是h,最低位是l,中间不变的部分为mid,由题意可得到下面的公式(这里对X乘上1e6用a表示,b表示1e6) (h ...
- 150 Evaluate Reverse Polish Notation 逆波兰表达式求值
求在 逆波兰表示法 中算术表达式的值.有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达.例如: ["2", "1&quo ...
- Map集合的实现类
Map的继承关系: Map接口的常用实现类: 1.HashMap.Hashtable(t是小写) HashMap不是线程安全的,key.value的值都可以是null. Hashtable是线程安全的 ...
- el-select,选择一行,取值行的对象.
<el-select v-model="set_invoice_form.InvoiceType" placeholder="请选择" :disabl ...
- oracle适配器连接不上解决方案
Oracle适配器连接不上解决方案 作者:Vashon oracle 的Developer连接不上报错:listener does not currently know of SID given in ...
- mac及windows下安装ss实现***
官网下载shadowsock(mac/windows都是下面地址,页面会根据当前系统自动判断所下载的包) https://sourceforge.net/projects/shadowsocksgui ...
- Linux 从源码编译安装 Nginx
Nginx 是一个高性能的 HTTP 和 反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器.Nginx 编译安装比较简单,难点在于配置.下面是 Nignx 0.8.54 编译安装和简 ...
- DedeCMS文章标题长度最全修改方法
有时候DedeCMS首页或者其他页面不能全部展示文章标题,造成读者阅读体验差.一般来说标题精简.概括性强.有本文关键词就是一个好标题.写软文不比写“作文”,也不是论坛的标题党,软文是用来做排名的,主要 ...
- MessageBox的使用
一 函数原型及参数 function MessageBox(hWnd: HWND; Text, Caption: PChar; Type: Word): Integer; hWnd:对话框父窗口 ...
- C ++ _基础之共用体
由以下代码来进一步学习共用体 #include <stdio.h> #include<iostream> void main() { union un { int a; cha ...