Spoj LCS suffix Automaton

Source: Internet
Author: User

Links: http://www.spoj.com/problems/LCS/

Test instructions two strings of LCS

There's really nothing to say, the first time. Let's turn over the online tutorial to see it again.

Another found that Baidu internal users to communicate the use of pictures in the Baidu snapshot.

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineMAXN 2510000#defineMaxt maxn*2structsam_node{intPnt,len; intnxt[ -]; voidPrint (intid=-1) {printf ("-----------------\ n"); printf ("Sam id:%d\n", id); printf ("Next: \ n");  for(intI=0;i< -; i++) printf ("[%c:%d];", i+'a', Nxt[i]); printf ("\ n"); printf ("parent:%d\n", PNT); printf ("length:%d\n", Len); }}SAM[MAXT];intlast=1;inttopt=1;voidAdd_item (Charch) {        intP,NP; P=Last ; NP=++topt; Sam[np].len=sam[p].len+1;  while(P &&!sam[p].nxt[ch-'a']) Sam[p].nxt[ch-'a']=np,p=Sam[p].pnt; if(!p) {SAM[NP].PNT=1; Last=NP; }Else        {                intq=sam[p].nxt[ch-'a']; if(sam[q].len==sam[p].len+1) Sam[np].pnt=Q; Else                {                        intNQ; NQ=++topt; SAM[NQ]=Sam[q]; Sam[nq].len=sam[p].len+1; SAM[NQ].PNT=Sam[q].pnt; SAM[Q].PNT=NQ; SAM[NP].PNT=NQ;  while(P && sam[p].nxt[ch-'a']==q) {sam[p].nxt[ch-'a']=NQ; P=Sam[p].pnt; }} last=NP; }}CharSTR[MAXN];CharSTR2[MAXN];intMain () {Freopen ("Input.txt","R", stdin); scanf ("%s", str); intn=strlen (str);  for(intI=0; i<n;i++) Add_item (Str[i]);  for(intI=0; i<=topt;i++)        {                //Sam[i]. Print (i);} scanf ("%s", STR2); intnow=1, res=0, ans=0; intm=strlen (STR2);  for(intI=0; i<m;i++)        {                if(sam[now].nxt[str2[i]-'a']) { now=sam[now].nxt[str2[i]-'a']; Res++; Ans=Max (ans,res); }Else                {                         while(Now &&!sam[now].nxt[str2[i]-'a']) now=Sam[now].pnt; if(!Now ) { Now=1; Res=0; }                        Else{res=sam[now].len+1; now=sam[now].nxt[str2[i]-'a']; Ans=Max (ans,res); }}} printf ("%d\n", ans);}

Spoj LCS suffix Automaton

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.