HDU 5384 AC automatic machine

Source: Internet
Author: User

AC automaton nude problem, really nude ...

The first n to match the string to save, and then the M-mode string structure of the AC automata, and then asked to be matched to the string is not ...

1#include <stdio.h>2#include <string.h>3#include <queue>4 using namespacestd;5 Const intmaxm=600006;6 7 Chars[100005][10005],word[10005];8 intnxt[maxm][ -],tail[maxm],f[maxm],size;9 intLAST[MAXM];Ten  One intNewNode () { Amemset (Nxt[size],0,sizeof(Nxt[size])); -f[size]=tail[size]=0; -     returnsize++; the } -  - voidInsertChars[]) { -     inti,p=0; +      for(i=0; s[i];i++){ -         int&x=nxt[p][s[i]-'a']; +p=x?x:x=NewNode (); A     } attail[p]++; - } -  - voidmakenxt () { -     inti; -queue<int>Q; inf[0]=0; -      for(i=0;i< -; i++){ to         intv=nxt[0][i]; +         if(v) { -f[v]=last[v]=0; the Q.push (v); *         } $     }Panax Notoginseng      while(!Q.empty ()) { -         intu=Q.front (); the Q.pop (); +          for(i=0;i< -; i++){ A             intv=Nxt[u][i]; the             if(!V) nxt[u][i]=Nxt[f[u]][i]; +             Else{ - Q.push (v); $f[v]=Nxt[f[u]][i]; $LAST[V]=TAIL[F[V]]?F[v]:last[f[v]]; -             } -         } the     } - }Wuyi  the intQueryChars[]) { -     intans=0, v=0; Wu      for(intI=0; s[i];i++){ -          while(v&&!nxt[v][s[i]-'a']) v=F[v]; Aboutv=nxt[v][s[i]-'a']; $         inttmp=v; -          while(TMP) { -ans+=tail[tmp]; -tmp=last[tmp]; A         } +     } the     returnans; - } $  the intMain () { the     intT; thescanf"%d",&T); the      while(t--){ -         intn,m; inscanf"%d%d",&n,&m); theSize=0, NewNode (); the          for(intI=0; i<n;++i) { Aboutscanf"%s", S[i]); the         } the          for(intI=0; i<m;++i) { thescanf"%s", word); + Insert (word); -         } the makenxt ();Bayi          for(intI=0; i<n;++i) { theprintf"%d\n", Query (S[i])); the         } -     } -     return 0; the}
View Code

HDU 5384 AC automatic machine

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.