病毒侵袭

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24393    Accepted Submission(s): 5862

Problem Description
当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻。。。。在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~
但网路上总有那么些网站,开始借着民众的好奇心,打着介绍日食的旗号,大肆传播病毒。小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
 

题目链接:HDU 2896

题意显然就是用AC自动机了,把病毒网站的源码放在Trie图上跑一遍,如果遇到病毒id则把这个病毒放入这个网站的vector中即可且不需要回溯,但由于病毒可能之间存在子串的关系,因此要用一个数组来记录当前网站是否出现过这个病毒,不能重复出现。后台数据不强导致不记录也能AC,给一组数据:

2
aaa
aaaa
2
aaaaaa
bbaacc

答案:

web 1: 1 2

total: 1

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int maxn = 510;
const int maxl = 210;
const int maxweb = 1010;
const int maxtar = 10010; char key[maxl], tar[maxtar];
bitset<maxn>vis;
vector<int>web[maxweb]; struct Aho
{
struct Trie
{
int nxt[128];
int id, fail;
inline void reset()
{
id = 0;
fail = 0;
}
};
Trie L[maxn * maxl];
int sz; void init()
{
sz = 1;
L[0].reset();
} void insert(char s[], int id)
{
int u = 0, len = strlen(s);
for (int i = 0; i < len; ++i)
{
int v = s[i];
if (!L[u].nxt[v])
{
L[sz].reset();
L[u].nxt[v] = sz++;
}
u = L[u].nxt[v];
}
L[u].id = id;
} void build()
{
L[0].fail = -1;
queue<int>Q;
Q.push(0);
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (int i = 0; i < 128; ++i)
{
int son = L[u].nxt[i];
if (!son)
continue;
Q.push(son);
if (!u)
L[son].fail = 0;
else
{
int f = L[u].fail;
while (~f)
{
if (L[f].nxt[i])
{
L[son].fail = L[f].nxt[i];
break;
}
f = L[f].fail;
}
if (f == -1)
L[son].fail = 0;
}
}
}
} void match(char s[], int id)
{
int u = 0, len = strlen(s);
for (int i = 0; i < len; ++i)
{
int v = s[i];
if (L[u].nxt[v])
u = L[u].nxt[v];
else
{
int f = L[u].fail;
while (~f && !L[f].nxt[v])
f = L[f].fail;
if (f == -1)
u = 0;
else
u = L[f].nxt[v];
}
if (L[u].id && !vis[L[u].id])
{
web[id].push_back(L[u].id);
vis[L[u].id] = 1;
}
}
}
};
Aho aho; void init()
{
for (int i = 0; i < maxweb; ++i)
web[i].clear();
aho.init();
}
int main(void)
{
int n, m, i, j;
while (~scanf("%d", &n))
{
init();
for (i = 1; i <= n; ++i)
{
scanf("%s", key);
aho.insert(key, i);
}
aho.build();
scanf("%d", &m);
for (i = 1; i <= m; ++i)
{
scanf("%s", tar);
vis.reset();
aho.match(tar, i);
}
int tot = 0;
for (i = 1; i <= m; ++i)
{
int sz = web[i].size();
if (!sz)
continue;
++tot;
sort(web[i].begin(), web[i].end());
printf("web %d: ", i);
for (j = 0; j < sz; ++j)
printf("%d%s", web[i][j], j == sz - 1 ? "\n" : " ");
}
printf("total: %d\n", tot);
}
return 0;
}

HDU 2896 病毒侵袭(AC自动机水)的更多相关文章

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

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

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

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

  3. hdu 2896 病毒侵袭 AC自动机 基础题

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

  4. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  5. hdu 2896 病毒侵袭_ac自动机

    题意:略 思路:套用ac自动机模板 #include <iostream> #include<cstdio> #include<cstring> using nam ...

  6. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

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

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

  8. HDU 2896 病毒侵袭【AC自动机】

    <题目链接> Problem Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋——我们能在有生之年看到500年一 ...

  9. hdu2896 病毒侵袭 ac自动机

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

随机推荐

  1. python_60_装饰器3

    #嵌套函数 def foo(): print('in the foo') def bar(): print('in the bar') bar() #bar()#出错,无法在外边调用,bar函数的作用 ...

  2. python_19_编码解码

    msg="我爱北京天安门" #字符串转成Byte类型 print(msg.encode())#encode 编码 print(msg.encode(encoding="u ...

  3. x5webview 微信H5支付

    mWebView.setWebViewClient(new WebViewClient() { // @Override // public boolean shouldOverrideUrlLoad ...

  4. Win8如何默认以管理员运行程序

    在Win7的时候,关闭UAC,使用自己的用户名,所有程序都是默认以管理员身份运行的. 但是在Win8,关闭UAC,程序不是默认以管理员身份运行的. 在论坛看到的解决方法是:1.用Administrat ...

  5. Mac 系统 + Chrome浏览器 网页前端出现中文文字反转或顺序错乱

    问题背景 React开发的系统,收到一个BUG反馈,*"号个人统计"文字不正确,应为"个人号统计"*. 收到BUG后,打开浏览器查验是什么情况,难道犯了最基本的 ...

  6. 安装配置JDK1.8开发环境以及配置java环境变量的步骤

    1.安装JDK开发环境 下载网站:http://www.oracle.com/ 开始安装JDK: 修改安装目录如下: 确定之后,单击“下一步”. 注:当提示安装JRE时,可以选择不要安装. 2.配置环 ...

  7. 十、MySQL 删除数据表

    MySQL 删除数据表 MySQL中删除数据表是非常容易操作的, 但是你再进行删除表操作时要非常小心,因为执行删除命令后所有数据都会消失. 语法 以下为删除MySQL数据表的通用语法: DROP TA ...

  8. java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServiceImpl

    java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to cn.service.impl.WorkinggServi ...

  9. requests.exceptions.SSLError……Max retries exceeded with url错误求助!!!

    import requests head = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Appl ...

  10. vi a.sh ABCD

    E: 无法定位软件包 ubuntu预装的是 vim tiny,安装vim full版本,可以解决 卸载vim-tiny: $ sudo apt-get remove vim-common 安装vim ...