Bzoj 3172 [Tjoi2013] word ac automaton (fail tree)

Source: Internet
Author: User

Test Instructions:Link Method: AC automata and Fail tree properties parsing:Review the first question of AC automata? (The real first question is clearly re-written all over the hdu2222! The question is to tell the truth that the first look is an SB problem, as long as the building from the motive, and then search the fail tree on the line, but after reading 140142 of the blog looks like this will t? But he's gone too, what the hell? Anyway, after thinking about what a good way to look at the solution, write the solution of Daniel's ideas can be summed up in a sentence, that is, the nature of the fail tree: you want to find the number of occurrences of a string is the sum of the number of times the root node of the string appears on the fail tree. Well know this nature after (can yy under), this problem can be another optimization, that is, in the build can be the whole tree in the BFS sequence to find out, and because the fail node is behind the connection to the front, so no effect, that is, we can from the stack of the stack top pumping elements, and add his end value to his fail node, which might be a lot faster? The output section is what we said earlier, find the root node of each string and ask for his end value. Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N#define M 1000100using namespace Std;intN,l,root,size;charsN [M];int NextM [ -],fail[m],end[m],Q[m];intNewNode () { for(intI=1; i<= -; i++)Next[Size] [i]=-1; end[size++]=0;returnsize-1;} void Init () {size=0; Root=newnode ();} void Ins () {intL=strlen (s[L]);intNow=root; for(intI=0; i<l;i++) {inttmp=sL [i]-' A '+1;if(Next[Now] [tmp]==-1)Next[Now]        [Tmp]=newnode (); now=Next[Now]        [TMP];    end[now]++; }}void Build () {intLl=0, rr=-1; for(intI=1; i<= -; i++) {if(Next[Root] [i]==-1)Next[Root] [I]=root;Else{fail[Next[Root] [I]] =root;Q[++RR]=Next[Root]        [i]; }    } while(LL&LT;=RR) {intu=q[ll++]; for(intI=1; i<= -; i++) {if(Next[U] [i]==-1)Next[U] [i]=Next[Fail[u]] [i];Else{fail[Next[U] [I]] =Next[Fail[u]] [i];Q[++RR]=Next[U]            [i]; }        }    } for(inti=rr;i>=0; i--) {end[fail[Q[i]]]+=end[Q[i]]; }}intMain () {init (); scanf"%d", &n); for(intI=1; i<=n;i++) {scanf ('%s',s[i]);        l++;    Ins ();    } build (); L=1; for(intI=1; i<=n;i++) {intK=root;intL=strlen (s[i]); for(intj=0; j<l;j++) k=NextK [sI [j]-' A '+1];printf("%d\ n", End[k]); }}

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

Bzoj 3172 [Tjoi2013] word ac automaton (fail 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.