HDU 3695-computer Virus on Planet Pandora (AC automaton)

Source: Internet
Author: User
Tags cmath

Test instructions

The sum of the number of occurrences of a string that is flipped after a parent string is given a string of strings and multiple patterns.

Analysis:

Template questions

/*#include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <algorithm >using namespace Std;const int maxnode = 250*1000+10000;const int sigma_size = 26;struct trie{int Ch[maxnode][sigma    _size];     int Val[maxnode];    The number of occurrences of the word in the pattern string int Last[maxnode];         int F[maxnode];     Mismatch array int Num[maxnode];    The number of times the word appears in the text string int pre[maxnode];    The precursor of the word int len[maxnode];     The word length at the end of the word int char[maxnode];   The word corresponds to the letter int Road[maxnode];    Path compression optimizes the number of species that appear for the calculation pattern string int sz;        int Newnode () {Val[sz] = F[sz] = Last[sz] = Len[sz] = Num[sz] = 0;        memset (Ch[sz], 0, sizeof Ch[sz]);    return sz++;        } void Init () {sz=0;    Newnode ();    } int idx (char c) {return C ' A ';}        int build (char *s) {int u = 0;            for (int i = 0, C; s[i]; i++) {c = idx (s[i]);            if (!ch[u][c]) ch[u][c] = Newnode ();            PRE[CH[U][C]] = u; CHAR[CH[U][C]] = S[i];            LEN[CH[U][C]] = len[u]+1;            ROAD[CH[U][C]] = 1;        U = ch[u][c];        } Val[u] = 1;        Num[u] = 0;    return u;        } void Getfail () {queue<int> q;        for (int i = 0; i<sigma_size; i++) if (Ch[0][i]) Q.push (Ch[0][i]);        int r, c, u, v;            while (!q.empty ()) {R = Q.front (); Q.pop ();                for (c = 0; c<sigma_size; C + +) {u = ch[r][c];                if (!u) continue;                Q.push (U);                v = f[r]; while (v && ch[v][c] = = 0) v = f[v];            Go up along the mismatch edge if there is a node after mismatch and its child node C exists then end loop f[u] = Ch[v][c];        }}} void Find (char *t) {//The number of occurrences of the calculation pattern string: (multiple occurrences per type) int j = 0; for (int i = 0, C, temp; T[i];            i++) {c = idx (T[i]);            while (J && ch[j][c]==0) j = f[j];            j = Ch[j][c];            temp = j;                while (temp) {num[temp]++;temp = f[temp];        }}}} int find_kind (char *t) {//calculates the number of species, repeated occurrences no longer calculated (if multiple queries are to be added here for (I=0->sz) lu[i]=1;        Int j = 0, I, C, temp,ans=0; for (i = 0; T[i];            i++) {c = idx (T[i]);            while (j && ch[j][c] = = 0) j = f[j];            j = Ch[j][c];            temp = j;                    while (temp && road[temp]) {if (Val[temp]) {++ans;                Val[temp] = 0;                } Road[temp] = 0;            temp = f[temp];    }} return ans;    }}ac;char s[1015], a[5100010], b[5100010], C;int N, num;int main () {int T; scanf ("%d", &t);        while (t-->0) {ac.init ();        scanf ("%d", &n);            while (n--) {scanf ("%s", s);        Ac.build (s);        } ac.getfail ();        int top = 0;        scanf ("%s", a);        int i = 0,sum=0,id=0;               while (a[i]!= ') {if (a[i]== ' [') { i++;                int len=0;                    while (a[i]>= ' 0 ' &&a[i]<= ' 9 ') {len=len*10+a[i]-' 0 ';                i++;                } for (int j=0;j<len;++j) b[id++]=a[i];            i+=2;                } else{B[id++]=a[i];            i++;        }} b[id]=0;        printf ("%s\n", b);        Sum+=ac.find_kind (b);        Reverse (B,b+id);        printf ("%s\n", b);        Sum+=ac.find_kind (b);    printf ("%d\n", sum); } return 0;}*/#include<map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<11#defineAll 1,n,1#defineRead Freopen ("In.txt", "R", stdin)#defineN 300010Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;Charp[1010],t[6000000],t1[6000000];intN;structtrie{intch[n][ -],val[n],f[n],num; voidinit () {num=1; memset (CH,0,sizeof(CH)); Memset (Val,0,sizeof(Val)); Memset (F,0,sizeof(f)); }    voidBuildChar*s) {        intu=0, len=strlen (s);  for(intI=0; i<len;++i) {intv=s[i]-'A'; if(!Ch[u][v]) {memset (Ch[num],0,sizeof(Ch[num])); CH[U][V]=num++; } u=Ch[u][v]; } Val[u]=1; //return u;    }    voidGetfail () {Queue<int>Q;  for(intI=0;i< -;++i)if(ch[0][i]) Q.push (ch[0][i]);  while(!Q.empty ()) {            intR=Q.front ();            Q.pop ();  for(intI=0;i< -;++i) {intu=Ch[r][i]; if(!u) {Ch[r][i] = Ch[f[r]][i];Continue;}                Q.push (U); intv=F[r];  while(V&&!ch[v][i]) v=F[v]; F[u]=Ch[v][i]; }        }    }    intFindChar*T) {        intu=0, Len=strlen (T), total=0;  for(intI=0; i<len;++i) {            intv=t[i]-'A';  while(u&&ch[u][v]==0) U=F[u]; U=Ch[u][v]; inttmp=u;  while(TMP) {if(Val[tmp]) { total+=Val[tmp]; VAL[TMP]=0; } tmp=F[tmp]; }        }        returnTotal ; }}ac;intMain () {intT; scanf ("%d",&t);  while(t--) {ac.init (); scanf ("%d",&N);  while(n--) {scanf ("%s", P);        Ac.build (P); } scanf ("%s", T); //printf ("%s", T);        intId=0, tid=0, i=0; CharTC;  while(t[i]!=' /'){            if(t[i]=='[') {i++; intlen=0;  while(t[i]>='0'&&t[i]<='9') {len=len*Ten+t[i]-'0'; I++; }                 for(intj=0; j<len;++j) T1[id++]=T[i]; I+=2; }            Else{T1[id++]=T[i]; I++; }} T1[id]=0; //printf ("%s\n", T1);        intsum=0; Sum+=Ac.find (T1); Reverse (t1,t1+ID); //printf ("%s\n", T1);sum+=Ac.find (T1); printf ("%d\n", sum); }return 0;}

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.