Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

Description

Input

Output

web 网站编号: 病毒编号 病毒编号 …

total: 带病毒网站数

Sample Input

3
aaa
bbb
ccc
2
aaabbbccc
bbaacc

Sample Output

web 1: 1 2 3
total: 1

Source

AC自动机。加一个数组，插入时记录特征码的编号，query时存在ans数组里，排序一下输出就行了，用vector比较好搞。

AC代码：
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int maxnode = 500 * 200 + 10;
struct trie
{
int next[maxnode][128], fail[maxnode], end[maxnode], num[maxnode];
int root, L;
int newnode()
{
memset(next[L], -1, sizeof(next[L]));
end[L++] = 0;
return L - 1;
}
void init()
{
L = 0;
root = newnode();
}
void insert(char buf[], int k)
{
int len = strlen(buf);
int now = root;
for (int i = 0; i < len; i++)
{
if (next[now][buf[i]] == -1)
next[now][buf[i]] = newnode();
now = next[now][buf[i]];
}
end[now]++;
num[now] = k;
}
void build()
{
queue <int> q;
fail[root] = root;
for (int i = 0; i < 128; i++)
if (next[root][i] == -1)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
q.push(next[root][i]);
}
while (!q.empty())
{
int now = q.front();
q.pop();
for (int i = 0; i < 128; i++)
if (next[now][i] == -1)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
q.push(next[now][i]);
}
}
}
bool query(char buf[], vector <int> &ans)
{
int len = strlen(buf);
int now = root;
bool res = 0;
for (int i = 0; i < len; i++)
{
now = next[now][buf[i]];
int temp = now;
while (temp != root)
{
if (num[temp] > 0)
{
res = 1;
ans.push_back(num[temp]);
}
temp = fail[temp];
}
}
return res;
}
}ac;
int n, m;
char tz[210], source[10010];
int main()
{
scanf("%d", &n);
ac.init();
for (int i = 1; i <= n; i++)
{
scanf("%s", tz);
ac.insert(tz, i);
}
ac.build();
scanf("%d", &m);
int cnt = 0;
vector <int> ans;
for (int i = 1; i <= m; i++)
{
ans.clear();
scanf("%s", source);
if (ac.query(source, ans))
{
cnt++;
sort(ans.begin(), ans.end());
printf("web %d: ", i);
bool first = 1;
for (int i = 0; i < ans.size(); i++)
{
if (!first)
printf(" ");
first = 0;
printf("%d", ans[i]);
}
printf("\n");
}
}
printf("total: %d\n", cnt);
return 0;
}