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