Test instructions
Give a maximum of 10 lengths of not more than 100000 strings, asking for the length of their LCS. The time limit is 0.25s of ghost animal.
Suffix Automaton practice ... Although some people say that, but I do not think the hash can be too.
The subject can be described as "the importance of updating the pre in a step order in Sam":
In general, the approach is somewhat similar to 1811-LCS, because there are multiple strings, so each string needs to run on the Sam once.
Record the maximum number of characters per node that can hold a length, and finally take Max.
The length of the substring that can be accommodated by the I point when the current string is matched with nans[i], and if the current answer on the pre of I is smaller than I, use nans[i] to update nans[i.pre].
Use ANS to represent the minimum value of all NaNs, update ans with NaNs when calculating each string, and finally find a max in ans.
First forget the words of the black body, the results of a long time and did not shoot out, submitted in the 10th Point WA is simply sad ...
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <iostream>5 #defineMAXN 105000*26 using namespacestd;7 8 structNode {ints[ -],pre; } SUF[MAXN];9 CharS[MAXN],T[MAXN];Ten intI,j,n,m,k,len1,len2,len,last =1, root =1, cnt =1; One intSTEP[MAXN],NANS[MAXN],ANS[MAXN],F[MAXN]; A - voidAddintNXT) - { the intnow = ++cnt, p =Last ; -Step[now] = Step[p] +1; - for(; P! =0&& SUF[P].S[NXT] = =0; p = suf[p].pre) SUF[P].S[NXT] =Now ; - if(p = =0) Suf[now].pre =Root; + Else - { + intQ =SUF[P].S[NXT]; A if(Step[q] = = Step[p] +1) Suf[now].pre =Q; at Else - { - intNQ = + +CNT; -STEP[NQ] = Step[p] +1; -SUF[NQ] =Suf[q]; -Suf[q].pre = Suf[now].pre =NQ; in for(; P! =0&& SUF[P].S[NXT] = = q; p = suf[p].pre) SUF[P].S[NXT] =NQ; - } to } +Last =Now ; - } the * BOOLcmpintAintb) {returnStep[a] <step[b];} $ Panax Notoginseng intMain () - { thescanf"%s\n", s+1); +Len1 = strlen (s+1); A for(i =1; I <= len1; i++) Add (s[i]-'a'); the + for(i =1; I <= CNT; i++) F[i] =i; -Stable_sort (f+1, f+1+cnt,cmp); $ $memcpy (Ans,step,sizeof(ans)); - - while(SCANF ("%s\n", t+1) !=EOF) the { -memset (NaNs,0,sizeof(NaNs));Wuyi theLen2 = strlen (t+1); - intnow = Root,len =0; Wu for(i =1; I <= len2; i++) - { About intNXT = t[i]-'a'; $ if(SUF[NOW].S[NXT]! =0) - { -len++; -now =SUF[NOW].S[NXT]; ANans[now] =Max (Nans[now],len); + } the Else - { $ while(SUF[NOW].S[NXT] = =0&& Now! =0) now =Suf[now].pre; the if(now = =0) now = Root,len =0; the ElseLen = Step[now] +1, now = Suf[now].s[nxt],nans[now] =Max (Nans[now],len); the } the } - for(i = CNT; I >=1; i--) in { theAns[f[i]] =min (Ans[f[i]],nans[f[i]]); theNans[suf[f[i]].pre] =Max (Nans[suf[f[i]].pre],nans[f[i]]); About } the } the the intANS =0; + for(i =1; I <= CNT; i++) -ANS =Max (ans,ans[i]); the Bayiprintf"%d\n", ANS); the return 0; the}
SPOJ 1812
"Suffix automaton" spoj 1812-lcsii