A KUANGBIN~~AC automatic version of the sub, with annotations, the role of counting.

Source: Internet
Author: User

The dictionary tree of the cock silk ... Drunk ...
#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<queue>#include<cmath>#include<cstdlib>UsingNamespace Std;struct Trie{int Next[500010][26],fail[500010],isend[500010];int root, L;IntNewNode(){For(int I=0; I<26; I++)This string of jumps empties next[L][i]=-1; Isend[L++]=0;Not a node layerReturn L-1;Returns the ordinal of this point}voidInit(){L=0; Root=NewNode();Give the root a point}voidInsert(Char buf[]){int Len=Strlen(buf);int now= root;For(int I=0; I< Len; I++){If(Next[Now][buf[I]-A]==-1)This character is not present in the current dictionary layer next[Now][buf[I]-A]=NewNode();Point to generate a new point, and this has the character now= Next[Now][buf[I]-A];Go to the new point to continue operation} isend[Now]++;This layer is a junction layer, labeled}voidBuild(){Queue<Int>q; Fail[Root]= root;The root node is still the root nodeFor(int I=0; I<26; I++)Traversal of the first characterIf(Next[Root][i]==-1)No this character starts with next[Root][i]= root;Jump to RootElseWith the beginning of this character{Fail[Next[Root][i]]= root;The failure pointer for this row bit is root Q.Push(Next[Root][i]);Line into queue} while (! Q.empty ())//the character {int now = Q.front ();//Layer out the first q.pop (); for (int i = 0;i < 26;i++)//For this line if (next[now][i] = = 1)//if the next Line does not have this character next[now][i] = next[fail[now]][i];//His next position, is back to this point the parent node of the next (back) else//If there is this character {fail[next[now][i]]=next[ fail[now]][i];//his next failure pointer to this position is the next Q.push (Next[now][i]) of this point parent node;//The Next line continues}}} int query (char buf[]) {int len = strlen (bu f); int now = root; int res = 0; for (int i = 0;i < len;i++) {now = next[now][buf[i]-' a '], int temp = now, while (temp! = root) {res + = isend[temp]; I Send[temp] = 0; temp = fail[temp]; }} return res; } void Debug () {for (int i = 0;i < l;i++) {printf ("id =%3d,fail =%3d,end =%3d,chi = [", I,fail[i],isend[i]); for (int j = 0;j < 26;j++) printf ("%2d", Next[i][j]); printf ("]\n"); }}};char buf[1000010]; Trie Ac;int Main () {int T; int n; scanf ("%d", &t), while (t--) {scanf ("%d", &n), Ac.init (); for (int i = 0;i < n ; i++) {scanf ("%s", buf), AC. Insert (BUF); } ac.build (); scanf ("%s", buf); printf ("%d\n", Ac.query (BUf)); } return 0;}

Pitch Kuangbin~~ac, with annotations, the role of counting.

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.