Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2896
Virus attacksTime Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)Total submission (s): 11796 accepted submission (s): 3067
Problem description when the light of the sun gradually becomes invisible to the Moon, the world loses its light, and the Earth begins with the darkest moment .... At this moment, people are excited-how happy we can see the wonders of the world we met in 500 in our lifetime ~~
However, there are always some websites on the Internet. With the curiosity of the people, the website began to spread viruses under the banner of introducing the eclipse. Little T unfortunately became one of the victims. So angry, he decided to find out all the websites with viruses in the world. Of course, everyone knows that this is impossible. Mr. T insisted on completing the task that he could not do. He said, "Son, son, sun, and sun are also helpless !" (Another employee has been born after yugong ).
Everything is hard at the beginning. Mr. T has collected a lot of virus signatures and the source code of a batch of strange websites. He wants to know which websites contain viruses, what virus does it carry? By the way, I want to know how many websites with viruses he has collected. At this time, he did not know how to start. So please help me. T is an acute problem, so the sooner the problem is solved, the better ~~
The first line of input, an integer N (1 <= n <= 500), indicates the number of virus signatures.
In the next n rows, each line indicates a virus pattern, and the character string length is between 20 and.
Each virus has a number, which is 1-n.
Virus signatures of different numbers are not the same.
In the next row, there is an integer m (1 <= m <= 1000), indicating the number of websites.
In the next M line, each line represents a website source code, and the source code string length is between and.
Each website has a ID (1-m.
The preceding strings contain both printable ASCII characters (excluding carriage return ).
Output is output in ascending order by website number in the following format: the website number with viruses and the website number containing viruses. Each line contains the information of a website containing viruses.
Web Site No.: virus no., virus No...
There is a space after the colon, and the virus numbers are arranged in ascending order. Two virus numbers are separated by a space. If a website contains viruses, the number of viruses cannot exceed 3.
The last line outputs statistics in the following format:
Total: number of websites with viruses
There is a space after the colon.
Sample Input
3aaabbbccc2aaabbbcccbbaacc
Sample output
web 1: 1 2 3total: 1
Source2009 multi-university training contest 10-host by nit
Question:
Read your questions.
Analysis:
Simple and crude templates for AC automatic machines.
/* * * Author : fcbruce * * Time : Sat 04 Oct 2014 07:39:54 PM CST * */#include <cstdio>#include <iostream>#include <sstream>#include <cstdlib>#include <algorithm>#include <ctime>#include <cctype>#include <cmath>#include <string>#include <cstring>#include <stack>#include <queue>#include <list>#include <vector>#include <map>#include <set>#define sqr(x) ((x)*(x))#define LL long long#define itn int#define INF 0x3f3f3f3f#define PI 3.1415926535897932384626#define eps 1e-10#ifdef _WIN32 #define lld "%I64d"#else #define lld "%lld"#endif#define maxm 128#define maxn 100007using namespace std;bool found[507];char T[10007];char P[233];int q[maxn<<2];struct ACauto{ int ch[maxn][maxm]; int val[maxn]; int nex[maxn]; int last[maxn]; int cnt; int sz; ACauto() { sz=1; val[0]=0; memset(ch[0],0,sizeof ch[0]); } void clear() { sz=1; val[0]=0; memset(ch[0],0,sizeof ch[0]); } int idx(const char ch) { return ch-'\0'; } void insert(const char *s,int v=1) { int u=0; for (int i=0;s[i]!='\0';i++) { int c=idx(s[i]); if (ch[u][c]==0) { val[sz]=0; memset(ch[sz],0,sizeof ch[sz]); ch[u][c]=sz++; } u=ch[u][c]; } val[u]=v; } void get_fail() { int f=0,r=-1; nex[0]=0; last[0]=0; for (int c=0;c<maxm;c++) { int u=ch[0][c]; if (u!=0) { nex[u]=0; q[++r]=u; last[u]=0; } } while (f<=r) { int x=q[f++]; for (int c=0;c<maxm;c++) { int u=ch[x][c]; if (u==0) continue; q[++r]=u; int j=nex[x]; while (j>0 && ch[j][c]==0) j=nex[j]; nex[u]=ch[j][c]; last[u]=val[nex[u]]>0?nex[u]:last[nex[u]]; } } } void calc(int j) { if (j!=0) { cnt++; found[val[j]]=true; calc(last[j]); } } void find(const char *T) { cnt=0; memset(found,0,sizeof found); for (int i=0,j=0;T[i]!='\0';i++) { int c=idx(T[i]); while (j>0 && ch[j][c]==0) j=nex[j]; j=ch[j][c]; if (val[j]!=0) calc(j); else if (last[j]!=0) calc(last[j]); } }}acauto;int main(){#ifdef FCBRUCE freopen("/home/fcbruce/code/t","r",stdin);#endif // FCBRUCE int n; while (scanf("%d",&n)==1) { acauto.clear(); for (int i=1;i<=n;i++) { scanf("%s",P); acauto.insert(P,i); } acauto.get_fail(); int m; scanf("%d",&m); int cnt=0; for (int i=1;i<=m;i++) { scanf("%s",T); acauto.find(T); if (acauto.cnt>0) { cnt++; printf("web %d:",i); for (int j=1;j<=n;j++) if (found[j]) printf(" %d",j); putchar('\n'); } } printf("total: %d\n",cnt); } return 0;}
HDU 2896 virus attack (AC automatic mechanism)