Viruses (AC automatic machine deformation)

Problem Description
When the sun is shining gradually obscured by the moon, the world lost the light, the earth to usher in the darkest moments. . . . At such moments, people are excited - we can see the 500 years of the wonders of the world in their lifetime, that is how happy thing ah ~ ~
but always has those sites on the web, start by curious people heart, name describes eclipse banner, wantonly spread the virus. Small t become one of the unfortunate victims. Small t so angry that he decided to take the world on all sites with the virus have to find out. Of course, everyone knows that this is impossible. T small but insisted it can not complete the task, he said: "The children and grandchildren infinite Kui also!" (Successors of the Foolish Old Man).
Everything is hard in the beginning, small t collect a lot of virus signatures, but also collected a number of strange source site, he wanted to know which of these sites is a virus, but also with what kind of virus? Incidentally, in the end he wanted to know how much the collection site with a virus. This time he did not know what the bite. So I would like to help out. T is a little impatient Oh, so to solve the problem as quickly as possible ohhh
 

 

Input
The first line, an integer N (1 <= N <= 500), represents the number of virus signatures.
Next N rows, each row represents a virus signature, signature string length between 20-200.
Each virus has a number, so to 1-N.
Different numbers of virus signatures will not be the same.
Following this line, there is an integer M (1 <= M <= 1000), represents the number of sites.
Next M rows, each row represents a site source, source string length between 7000-10000.
Every website has a number, and so is 1-M.
In the above character strings are visible ASCII code characters (not including the carriage return).
 

 

Output
Followed by format output following site by site ID number and contains a virus from small to large output number, with the virus, one per line toxic site information.
No web site: No virus virus numbers ...
there is a space after the colon, the virus numbered in ascending order, between two numbers separated by a space virus, if a site contains a virus, the virus does not number more than three.
The last line of output statistics, in the following format
total: the number of websites with the virus
after the colon there is a space.
 

 

Sample Input
3 aaa bbb ccc 2 aaabbbccc bbaacc
 

 

Sample Output
Web 1: 1 2 3 Total: 1
 

 

Source
 

 

Recommend
gaojie   |   We have carefully selected several similar problems for you:   3065  2243  2825  3341  3247 
 
 
  1 #include <stdio.h>
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <string.h>
  5 #include <stdlib.h>
  6 #include <math.h>
  7 #include <queue>
  8 #include <set>
  9 
 10 #define INF 0x3f3f3f3f
 11 #define pii pair<int,int>
 12 using namespace std;
 13 
 14 
 15 
 16 
 17 struct ac_automation{
 18     int next[210*500][128],fail[210*500],end[210*500];
 19     int root,L;
 20     bool vis[510];
 21     int newnode(){
 22         for (int i=0;i<128;i++)
 23         {
 24             next[L][i] = -1;
 25         }
 26         end[L++] = 0;
 27         return L-1;
 28     }
 29     void init(){
 30         L = 0;
 31         root = newnode();
 32     }
 33     void insert(char s[],int id){
 34         int len = strlen(s);
 35         int now = root;
 36         for (int i=0;i<len;i++){
 37             if (next[now][s[i]] == -1)
 38                 next[now][s[i]] = newnode();
 39             now = next[now][s[i]];
 40         }
 41         end[now] = id;
 42     }
 43     void build(){
 44         queue<int > Q;
 45         fail[root] = root;
 46         for (int i=0;i<128;i++)
 47         {
 48             if (next[root][i] == -1)
 49                 next[root][i] = root;
 50             else{
 51                 fail[next[root][i]] = root;
 52                 Q.push(next[root][i]);
 53             }
 54         }
 55         while (!Q.empty()){
 56             int now = Q.front();
 57             Q.pop();
 58             for (int i=0;i<128;i++){
 59                 if (next[now][i] == -1)
 60                     next[now][i] = next[fail[now]][i];
 61                 else{
 62                     fail[next[now][i]] = next[fail[now]][i];
 63                     Q.push(next[now][i]);
 64                 }
 65             }
 66         }
 67     }
 68     bool querry(char buf[],int n,int id){
 69         int len = strlen(buf);
 70         int now = root;
 71         bool flag = false;
 72         memset(vis,false, sizeof(vis));
 73         for (int i=0;i<len;i++){
 74             now = next[now][buf[i]];
 75             int temp = now;
 76             while (temp!=root){
 77                 if (end[temp]!=0){
 78                     vis[end[temp]] = true;
 79                     flag = true;
 80                 }
 81                 temp = fail[temp];
 82             }
 83         }
 84         if (!flag)
 85             return false;
 86         printf("web %d:",id);
 87         for (int i=1;i<=n;i++)
 88         {
 89             if (vis[i])
 90                 printf(" %d",i);
 91         }
 92         printf("\n");
 93         return true;
 94     }
 95 };
 96 ac_automation ac;
 97 char buf[10010];
 98 int main(){
 99     int n,m;
100     while (scanf("%d",&n)!=EOF){
101         ac.init();
102         for (int i=1;i<=n;i++){
103             scanf("%s",buf);
104             ac.insert(buf,i);
105         }
106         ac.build();
107         int ans = 0;
108         scanf("%d",&m);
109         for (int i=1;i<=m;i++){
110             scanf("%s",buf);
111             if (ac.querry(buf,n,i))
112                 ans++;
113         }
114         printf("total: %d\n",ans);
115     }
116     return 0;
117 }

 

Guess you like

Origin www.cnblogs.com/-Ackerman/p/11324276.html