Bnuoj 7178 virus is being attacked

Source: Internet
Author: User
Time Limit: 1000 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 3065
64-bit integer Io format: % i64d Java class name: Main small T thanks for your help in solving his previous problem. However, virus attacks continue. With the unremitting efforts of Mr. T, he discovered the "source of evil" in the Internet ". This is a huge virus website. It has many viruses, but the website contains very strange viruses. These viruses have very short signatures and only contain "uppercase letters ". Of course, little t really wants to harm the people, but little t never fights an unprepared war. Know yourself and know what you are doing. The first thing small T wants to do is to know the characteristics of this virus Website: How many different viruses are contained, and how many times each virus appears. Can you help him? The first line of input, an integer N (1 <= n <= 1000), indicates the number of virus signatures.
In the next n rows, each line represents a virus pattern. The character string must be 1-50 characters long and only contain "uppercase letters ". No two virus signatures are identical.
In the next line, it indicates the source code of the website "the source code of all evil". The source code string length is within 2000000. The characters in the string are both visible ASCII characters (excluding carriage return ).
Output is one per line in the following format, and the number of times each virus appears. No output is required for a virus that does not appear.
Virus pattern: number of occurrences
There is a space after the colon, Which is output in the input order of the virus signature.
Sample Input
3AABBCCooxxCC%dAAAoen....END
Sample output
AA: 2CC: 1
HintHit: all situations not mentioned in the title description should be considered. For example, two virus signatures may contain one another or overlap one another. The counting policy can also be inferred to some extent from the sample. Source2009 multi-university training contest 16-host by nit solution: AC automatic machine .....
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define INF 0x3f3f3f3f15 using namespace std;16 struct trie{17     int wd[28],fail,index;18     void init(){19         memset(wd,0,sizeof(wd));20         fail = index = 0;21     }22 };23 trie dic[100000];24 int tot,n,cnt[1010];25 char word[1010][60];26 char web[2001000];27 void insertWd(int root,int id,char *s){28     for(int i = 0; s[i]; i++){29         int k = s[i]-‘A‘;30         if(!dic[root].wd[k])31             dic[root].wd[k] = tot++;32         root = dic[root].wd[k];33     }34     dic[root].index = id;35 }36 void build(int root){37     queue<int>q;38     q.push(root);39     int u,v,i,j;40     while(!q.empty()){41         u = q.front();42         q.pop();43         for(i = 0; i < 28; i++){44             if(!dic[u].wd[i]) continue;45             if(!u) dic[dic[u].wd[i]].fail = 0;46             else{47                 v = dic[u].fail;48                 while(v && !dic[v].wd[i]) v = dic[v].fail;49                 if(dic[v].wd[i]) dic[dic[u].wd[i]].fail = dic[v].wd[i];50                 else dic[dic[u].wd[i]].fail = 0;51             }52             q.push(dic[u].wd[i]);53         }54     }55 }56 void query(int root,char *s){57     for(int i = 0; s[i]; i++){58         int k = s[i]-‘A‘;59         if(k < 0 || k > 26) {root = 0;continue;}60         while(root && !dic[root].wd[k]) root = dic[root].fail;61         root = dic[root].wd[k];62         if(root){63             int v = root;64             while(v && dic[v].index){65                 cnt[dic[v].index]++;66                 v = dic[v].fail;67             }68         }69     }70 }71 int main() {72     int i,root;73     while(~scanf("%d",&n)){74         tot = 1;75         root = 0;76         for(i = 0; i < 100000; i++)77             dic[i].init();78         memset(cnt,0,sizeof(cnt));79         for(i = 1; i <= n; i++){80             scanf("%s",word[i]);81             insertWd(root,i,word[i]);82         }83         build(root);84         scanf("%s",web);85         query(root,web);86         for(i = 1; i <= n; i++){87             if(cnt[i]) printf("%s: %d\n",word[i],cnt[i]);88         }89     }90     return 0;91 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.