个人博客→TanJX的自留地
病毒侵袭
Time limit 1000 ms Memory limit32768 kB
当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻。。。。在这样的时刻,人们却异常兴奋——我们能在有生之年看到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
题意
中文题
题解
AC自动机。。。这一直MLE,真是心痛的啊。幸亏gyf告诉我,释放内存,顺便换了c++,才A了,最骚的是32696kB飘过
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <queue>
using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define lson rt<<1
#define rson rt<<1|1
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
typedef long long ll;
const int maxn = 10100;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
int n;
int sum = 0;
char str[maxn];
struct Trie{
int id;
int count;
Trie *fail;
Trie *Next[128];
Trie(){
id = 0;
count = 0;
fail = NULL;
me(Next,NULL);
}
} *root;
void build_tire_tree(char s[],int k){
int len = strlen(s);
Trie *temp = root;
for(int i = 0; i < len; i++){
int index = s[i];
if(temp->Next[index] == NULL){
temp->Next[index] = new Trie();
}
temp = temp->Next[index];
}
temp->count++;
temp->id = k;
}
void build_ac_automation(){
queue<Trie*> Q;
Q.push(root);
root->fail = NULL;
while(!Q.empty()){
Trie *temp = Q.front();
Trie *p = NULL;
Q.pop();
for(int i = 0; i < 128; i++){
if(temp->Next[i] != NULL){
if(temp == root){
temp->Next[i]->fail = root;
}
else{
p = temp->fail;
while(p != NULL){
if(p->Next[i] != NULL){
temp->Next[i]->fail = p->Next[i];
break;
}
p = p->fail;
}
if(p == NULL){
temp->Next[i]->fail = root;
}
}
Q.push(temp->Next[i]);
}
}
}
}
void query(char s[],int k){
bool flag = false;
int dd[550];
me(dd,0);
int v = 0;
int len = strlen(s);
Trie *temp = root;
Trie *p = NULL;
for(int i = 0; i < len; i++){
int index = s[i];
while(temp->Next[index] == NULL && temp != root){
temp = temp->fail;
}
temp = temp->Next[index];
if(temp == NULL){
temp = root;
}
p = temp;
while(p != root && p->id != 0){
dd[v++] = p->id;
p = p->fail;
flag = true;
}
}
if(flag){
sum++;
}
if(dd[0] != 0){
sort(dd,dd+v);
printf("web %d:",k);
for(int i = 0; i < v; i++){
printf(" %d",dd[i]);
}
cout<<endl;
}
}
void dfs(Trie *p)
{
for(int i = 0;i < 128; i++)
{
if(p->Next[i])
dfs(p->Next[i]);
}
free(p);
}
int main()
{
int n;
char s[maxn];
cin>>n;
root = new Trie();
for(int i = 1; i <= n; i++){
scanf("%s",s);
build_tire_tree(s,i);
}
build_ac_automation();
int m;
cin>>m;
for(int i = 1; i <= m; i++){
scanf("%s",str);
query(str,i);
}
printf("total: %d\n",sum);
dfs(root);
return 0;
}