AC Own Active machine template (array implementation version)

Source: Internet
Author: User

by Nine Wild

Did a problem, with my kind of style gorgeous time out. , but learned to learn the array implementation of the

#include <stdio.h> #include <string.h> #include <queue> #include <iostream>using namespace std; const INT maxnode=250*1000+10000;//maxnode= Word number * Word length + constant const int Sg_size=26;struct trie{int Ch[maxnode][sg_size];int val[maxnode];//the number of occurrences of the word in the pattern string int last[maxnode];int f[maxnode];//mismatch function int num[maxnode];//The number of occurrences of the word in the text string int Pre[maxnode] ;//The word's precursor int len[maxnode];//the word length int char[maxnode];//the word with the corresponding letter int road[maxnode];//path compression optimization for the type of pattern string that appears 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 Insert (char *s) {int u=0,i;for (i=0;s[i];i++) {int c=idx (s[i]), if (!ch[u][c]) Ch[u][c]=newnode ();p re[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;int i;for (i=0;i<sg_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<sg_size;c++) {u=ch[R][c];if (U) continue;q.push (u), V=f[r];while (v&&ch[v][c]==0) v=f[v];//along the mismatch side, assuming that there is a node after mismatch and that its child node C exists, the end loop f[u]=ch [V] [C];}}} void Find (char *s) {//number of occurrences of the calculation pattern string: (multiple occurrences per type) int j=0;for (int i=0;s[i];i++) {int c=idx (s[i]); while (j&&ch[j][c]= =0) j=f[j];j=ch[j][c];int Temp=j;while (temp) {num[temp]++;temp=f[temp];}}}   void Find_kind (char *s,int &ans) {//calculates the number of species, recurring no longer calculated (if multiple queries are here to add for (I=0->sz) lu[i]=1; int J=0,i,c,temp;for (i=0;s[i];i++) {c=idx (s[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];}}} Ac;int main () {}


AC Own Active machine template (array implementation version)

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.