Hdoj 3065 virus attack persists (AC automaton)

Source: Internet
Author: User

Topic Links: http://acm.hdu.edu.cn/showproblem.php?pid=3065

Thinking Analysis: the problem requires patterns to match multiple pattern strings, it should be noted that the pattern string will contain and overlap, need to modify the matching process of AC automata, for each node, need from the node's failure pointer backtracking,

If the node after the failed pointer backtracking is the last node of a pattern string, another pattern string is matched;

The code is as follows:

#include <queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intKIND = -;Const intMax_node =1020* -;Const intMax_n = ++Ten;Const intMax_m =2000000+ -;CharStr[max_m];intVisited[max_n];intVir_match[max_n];Charvir[max_n][ -];structTrie {introot, Count; intNext[max_node][kind], Fail[max_node], End[max_node]; voidInit () {count=0; Root=NewNode (); }    intNewNode () { for(inti =0; i < KIND; ++i) next[count][i]= -1; End[count]= -1; returncount++; }    voidInsert (Char*STR,intID) {inti =0, k =0; intnow =Root;  while(Str[i]) {k=Str[i]; if(Next[now][k] = =-1) Next[now][k]=NewNode (); now=Next[now][k]; ++i; } End[now]=ID; }    voidBuildautomaton () {Queue<int>Q; Fail[root]= -1;        Q.push (root);  while(!Q.empty ()) {            intnow =Q.front (); intp =-1;            Q.pop ();  for(inti =0; i < KIND; ++i) {if(Next[now][i]! =-1)                {                    if(now = =root) fail[next[now][i]]=Root; Else{p=Fail[now];  while(P! =-1)                        {                            if(Next[p][i]! =-1) {Fail[next[now][i]]=Next[p][i];  Break; } P=Fail[p]; }                        if(p = =-1) Fail[next[now][i]]=Root;                } q.push (Next[now][i]); }            }        }    }    intMatch (Char*str) {        inti =0, k =0, Vir_count =0; intp =Root;  while(Str[i]) {k=Str[i];  while(Next[p][k] = =-1&& P! =root) P=Fail[p]; P=Next[p][k]; P= (P = =-1) ?root:p; inttemp =p;  while(Temp! =root) {                if(End[temp]! =-1)                {                    if(Visited[end[p]] = =0) Vir_match[vir_count++] =End[p]; VISITED[END[P]]++; } temp=Fail[temp]; }            ++i; }        returnVir_count; }}; Trie Root;intMain () {intVir_num =0; intMatch_count =0;  while(SCANF ("%d\n", &vir_num)! =EOF) {root.        Init (); memset (Vir_match,0,sizeof(Vir_match)); Memset (visited,0,sizeof(visited));  for(inti =0; i < Vir_num; ++i) {gets (str);            strcpy (Vir[i], str); Root. Insert (str, I+1); } Match_count=0; Root.        Buildautomaton ();        Gets (str); intAns =Root.        Match (str); Sort (Vir_match, Vir_match+ans); if(ans) { for(intj =0; J < ans-1; ++j) printf ("%s:%d\n", Vir[vir_match[j]-1], Visited[vir_match[j]]); printf ("%s:%d\n", Vir[vir_match[ans-1] -1], Visited[vir_match[ans-1]]); }    }    return 0;}

Hdoj 3065 virus attack persists (AC automaton)

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.