Hdu2222ac automaton or bare dictionary tree

Source: Internet
Author: User

AC Automaton Code

#include <cstdio>#include <iostream>#include <queue>#include <cstring>using namespace STD;#define ID (a) (A-' a ')Const intMaxnode=10000* -+Ten;intch[maxnode][ -];intF[maxnode];intVal[maxnode];intSzintInsertChar*s) {intU =0; for(inti =0; s[i];i++) {intc = ID (s[i]);if(!ch[u][c]) {memset(Ch[sz],0,sizeof(Ch[sz])); VAL[SZ] =0; F[SZ] =0;        CH[U][C] = sz++;    } u = Ch[u][c]; } Val[u] + +;}intFind_fail () {intU =0; Queue<int>Que f[0]=0; for(inti =0; I < -; i++) {intU = ch[0][i];if(u) {F[u] =0;                Que.push (U); }    } while(!que.empty ()) {intR = Que.front (); Que.pop (); for(inti =0; I < -; i++) {if(!ch[r][i])            {Ch[r][i] = Ch[f[r]][i]; }Else{F[ch[r][i]] = ch[f[r]][i];             Que.push (Ch[r][i]); }        }    }}intQueryChar*s) {intU =0;intAns =0; for(inti =0; s[i];i++) {u = ch[u][id (S[i])];intTMP = u; while(TMP)            {ans + = val[tmp]; VAL[TMP] =0;        TMP = f[tmp]; }    }printf("%d\n", ans);returnAns;}Chars[1000005];Chara[ -];intMain () {intTCin>> T; while(t--) {intNCin>> N; SZ =1;memset(ch[0],0,sizeof(ch[0])); val[0]=0; f[0] =0; for(inti =0; i < n;i++) {scanf('%s ', a);        Insert (a); } find_fail ();scanf('%s ', s);    query (s); }}

Bare trie Code

#include <cstdio>#include <iostream>#include <algorithm>#include <queue>#include <cmath>#include <cstring>#include <stack>#include <set>#include <map>#include <vector>using namespace STD;#define INF 0X2FFFFFFF#define LL Long Long#define MAX (A, B) ((a) > (a))? (a):(B)#define MIN (a) < (b))? (a):(B)#define ID (a) ((a)-' a ')Const intMaxnode =10000* -+Ten;intch[maxnode][ -];intVal[maxnode];intSZ =0;intInsertChar*s) {intLen =strlen(s);intU =0; for(inti =0; i < len;i++) {intc = ID (s[i]);if(!ch[u][c]) {memset(Ch[sz],0,sizeof(Ch[sz])); VAL[SZ] =0;        CH[U][C] = sz++;               } u = Ch[u][c]; } Val[u] + +;}intQueryChar*s) {intAns =0; for(inti =0; s[i];i++) {intc = ID (s[i]);intU =0;intj =0; while(Ch[u][c]) {if(Val[ch[u][c]])                {ans + = val[ch[u][c]]; VAL[CH[U][C]] =0;            } u = Ch[u][c];            j + +;        c = ID (s[i+j]); }    }printf("%d\n", ans);}Chars[1000005];intMain () {intT Freopen ("1.txt","R", stdin); Freopen ("2.txt","W", stdout);scanf("%d", &t); while(t--) {intNscanf("%d", &n);Chara[ -];memset(ch[0],0,sizeof(ch[0])); val[0]=0; SZ =1; for(inti =0; i < n;i++) {scanf('%s ', a);        Insert (a); }scanf('%s ', s);    query (s); }return 0;}

Bare dictionary tree when as long as more than 600 ms, write AC automaton but with more than 700 MS, and start when do not know where wrong, has been wa,tle,re,mle, where is the AC automaton ah, is clearly WA automatic machine, tle automaton, re automatic machine, MLE automaton Ah!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu2222ac automaton or bare dictionary tree

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.