Construction of "template consolidation" Sam suffix automata

Source: Internet
Author: User

It's too weak Qaq learned Sam so long to learn to build Qaq

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#define MAXN 200010using namespace STD;structsam{intP,Q,NP,NQ;intCnt,last;inta[maxn][ -],LEN[MAXN],FA[MAXN];    Sam () {last = ++cnt; }voidInsertintCH) {p = LAST;NP = last = ++cnt;l[np] = L[p] +1;//Constantly add the transfer from the current state to the NP until a state p is found to have been present about the transfer of the character CH, after the turn state for Q         while(!a[p][ch] && p) a[p][ch] = Np,p = Fa[p];if(!p) FA[NP] =1;//It is possible that this state is the initial state of the automaton        Else{q = a[p][ch];//Status Q is State p obtained after CH transfer            //For discussion, there are two cases of len_q=len_p+1,len_q>len_p+1.            if(Len[q] = = Len[p] +1) FA[NP] = q;//At this time directly to make fa_np=q            Else{NQ = ++cnt;len[nq] = Len[p] +1;//Otherwise establish a new state nq,len_nq=len_p+1                //NQ represents the string of the original string in length not exceeding len_p+1, its FA and transfer are consistent with Q                memcpy(A[nq],a[q],sizeof(A[q]));                FA[NQ] = Fa[q]; FA[NP] = fa[q] = NQ; while(A[p][ch] = = q) a[p][ch]=q,p=fa[p]; }         }    }voidBuild () {CharCH[MAXN];scanf('%s ', ch); for(inti =0; I <strlen(ch); i++) Insert (Ch[i]-' A ');//build suffix automata for known strings}}/*int Main () {}*/

My code comments have always been enough to see >_<
You can see that Sam was built by inserting characters to build it.
And because of this, Sam is able to do other inline characters that are not supported by string data structures.
This also gives us a hint:
If you face a string of questions, the template string will be inserted into the character online or the topic requires mandatory online, and the data range tells you not to be able to re-preprocess to update the new version of the data structure, the problem must be Sam undoubtedly.

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

Construction of "template consolidation" Sam suffix automata

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.