HDU 3695 computer Virus on Planet Pandora (AC automaton)

Source: Internet
Author: User

Topic Connection: HDU 3695 computer Virus on Planet Pandora

Topic: Given some virus strings, it is required to judge that a given string contains several virus strings, including inversion.

How to solve the problem: Expand the given string, then match it once and then invert it once.

#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <iostream>#include <algorithm>using namespace STD;Const intMAXN =250000;Const intSigma_size = -;structAho_corasick {intSZ, g[maxn][sigma_size];intTAG[MAXN], FAIL[MAXN], LAST[MAXN];intvis[ -], jump[ -];voidInit ();intIdxCharCH);voidInsertChar* STR,intk);voidGetfail ();voidMatchChar* str);voidPutintu);intSolveChar* s);} AC;intNCharw[1005], s[5100005];intMain () {intCAsscanf("%d", &cas); while(cas--) {Ac.init ();scanf("%d", &n); for(inti =1; I <= N; i++) {scanf('%s ', W);        Ac.insert (w, i); }intMV =0, X;CharCh GetChar (); while(ch = getchar (), ch! =' \ n ') {if(ch = =' [') {scanf("%d%c", &x, &ch); for(inti =0; i < x;                i++) s[mv++] = ch;            GetChar (); }Elses[mv++] = ch; } S[MV] =' + ';printf("%d\n", Ac.solve (s)); }return 0;}intAho_corasick::solve (Char* s) {memset(Vis,0,sizeof(VIS));    Ac.getfail ();    Ac.match (s); Reverse (s, S +strlen(s)); Ac.match (s);intAns =0; for(inti =1; I <= N; i++)if(Vis[jump[i]]) ans++;returnAns;}voidAho_corasick::init () {sz =1; tag[0] =0;memset(g[0],0,sizeof(g[0]));}intAho_corasick::idx (CharCH) {returnCH-' A ';}voidAho_corasick::p ut (intu) {if(Vis[tag[u]])return; Vis[tag[u]] =1;if(Last[u]) put (Last[u]);}voidAho_corasick::insert (Char* STR,intK) {intU =0, n =strlen(str); for(inti =0; I < n; i++) {intv = idx (str[i]);if(G[u][v] = =0) {Tag[sz] =0;memset(G[sz],0,sizeof(G[sz]));        G[U][V] = sz++;    } u = G[u][v]; }if(Tag[u] = =0) Tag[u] = k; JUMP[K] = Tag[u];}voidAho_corasick::match (Char* Str) {intn =strlen(str), u =0; for(inti =0; I < n; i++) {intv = idx (str[i]); while(U && g[u][v] = =0) u = fail[u]; U = g[u][v];if(Tag[u]) put (U);Else if(Last[u]) put (last[u]); }}voidAho_corasick::getfail () { Queue<int>Que for(inti =0; i < sigma_size; i++) {intU = g[0][i];if(u) {fail[u] = last[u] =0;        Que.push (U); }    } while(!que.empty ()) {intR = Que.front (); Que.pop (); for(inti =0; i < sigma_size; i++) {intU = g[r][i];if(U = =0) {G[r][i] = G[fail[r]][i];Continue; } que.push (U);intv = fail[r]; while(v && g[v][i] = =0) v = fail[v];            Fail[u] = G[v][i]; Last[u] = Tag[fail[u]]?        Fail[u]: Last[fail[u]; }    }}

HDU 3695 computer Virus on Planet Pandora (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.