The virus continues to attack

Source: Internet
Author: User

Little T thank you very much for helping to solve his last problem. However, the virus continues to invade. Under the tireless efforts of little T, he discovered the "root of all evils" in the internet. This is a huge virus website, he has a lot of virus, but this site contains a very strange virus, the signatures of these viruses are very short, and only contain "English characters". Of course small T good want to people from harm, but little t never fight unprepared war. The enemy, Baizhanbudai, small T first to do is to know the virus site features: How many different viruses, each virus appears how many times. Can we help him again? Input first line, an integer N (1<=n<=1000) that represents the number of virus signatures.
Next n rows, each line represents a virus signature, the signature string length is between 1―50, and contains only the English uppercase characters. Any of the two virus signatures will not be identical.
After this line, said the "root of All Evil" website source code, the source string length within 2000000. Characters in the string are ASCII-visible characters (not including carriage returns). Output

Output each virus occurrence in the following format, one per line. No output is required for viruses that do not appear.

virus Signature: Number of occurrences
A colon is followed by a space that is output in the order in which the virus signature is entered.
 
Analysis: The topic of AC automata. The problem of AC automata is related to the time complexity and the topic. If you still need to traverse the fail pointer up to the root after finding a match point, the time complexity is O (s*h). where M is the height of the trie. If you do not need to traverse to the root along the fail, the time complexity is O (h*n+s). When the AC automaton is implemented, the NXT pointer can be used all together. Trie[r].nxt[i] If equal to 0, then Trie[r].nxt[i]=trie[trie[r].fail].nxt[i]. Speed can be a little bit faster.
#include <iostream>#include<cstdio>#include<cstring>#defineMAXC 26#defineMAXS 1002#defineMAXN 2000005#defineMAXL 51CharWORD[MAXS][MAXL],WEB[MAXN];structnode{intNxt[maxc],fail,id; voidClear () {memset (NXT,0,sizeofNXT); Fail=0, id=0; }}trie[maxl*Maxs];intmyq[maxl*maxs],head,tail,root,tot=1, Ans[maxs],n;voidInsertintRChar*s,intID) {    intlen=strlen (s);  for(intI=0; i<len;i++)    {        if(trie[r].nxt[s[i]-'A']==0) {Trie[r].nxt[s[i]-'A']=++tot;            Trie[tot].clear (); } R=trie[r].nxt[s[i]-'A']; } trie[r].id=ID;}voidBuildintR) {Trie[r].fail=R; Myq[tail++]=R; intch;  while(head<tail) {R=myq[head++]; if(r==root) {            for(intI=0; i<maxc;i++)             {             if(trie[r].nxt[i]==0) Trie[r].nxt[i]=Root; Else{Myq[tail++]=Trie[r].nxt[i]; Trie[trie[r].nxt[i]].fail=Root; }            }        }        Else        {         for(intI=0; i<maxc;i++) {ch=Trie[r].nxt[i]; if(CH) {Myq[tail++]=ch; if(trie[trie[r].fail].nxt[i]!=ch) trie[ch].fail=Trie[trie[r].fail].nxt[i]; ElseTrie[ch].fail=Root; }            ElseTrie[r].nxt[i]=Trie[trie[r].fail].nxt[i]; }        }    }}voidQueryintRChar*1) {    intlen=strlen (s), failp,val;  for(intI=0; i<len;i++) {Val=s[i]-'A'; if(val<0|| Val> -) {R=root;Continue;} R=Trie[r].nxt[val];  for(failp=r;failp!=root;failp=trie[failp].fail)if(trie[failp].id) ans[trie[failp].id]++; }}intMain () { while(~SCANF ("%d",&N)) {root=1; tot=1; Head=tail=0; memset (ans,0,sizeofans);    Trie[root].clear ();  for(intI=0; i<n;i++) {scanf ("%s", Word[i]); Insert (Root,word[i],i+1);    } build (root); scanf ("%s", web);    Query (Root,web);  for(intI=1; i<=n;i++)    {        if(Ans[i]) printf ("%s:%d\n", word[i-1],ans[i]); }    }}

The virus continues to attack

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.