标签:eof style clu lin ace har lcs poj class
题面
https://vjudge.net/problem/SPOJ-LCS
题解
#include<cstdio> #include<iostream> #include<cstring> #define ri register int #define N 100050 using namespace std; char s1[N],s2[N]; struct SAM{ int ff[N<<1],len[N<<1]; int ch[N<<1][26]; int last,tot; inline void clear() { last=tot=1; memset(ch,0,sizeof(ch)); } inline void extend(int c) { int p,np,q,nq; p=last;np=++tot;last=tot;len[np]=len[p]+1; while(p && !ch[p][c]) ch[p][c]=np,p=ff[p]; if (!p) ff[np]=1; else { q=ch[p][c]; if (len[q]==len[p]+1) { ff[np]=q; } else { nq=++tot; len[nq]=len[p]+1; for (ri i=0;i<26;i++) ch[nq][i]=ch[q][i]; ff[nq]=ff[q]; ff[np]=ff[q]=nq; while (p && ch[p][c]==q) ch[p][c]=nq,p=ff[p]; } } } inline int match() { int ret=0; int cnt=0,now=1; for (ri i=1,l=strlen(s2+1);i<=l;i++) { if (ch[now][s2[i]-‘a‘]) { cnt++; now=ch[now][s2[i]-‘a‘]; ret=max(ret,cnt); } else { while (now && !ch[now][s2[i]-‘a‘]) now=ff[now]; if (!now) { cnt=0; now=1; continue; } else { cnt=len[now]+1; ret=max(ret,cnt); now=ch[now][s2[i]-‘a‘]; } } } return ret; } } sam; int main(){ scanf("%s%s",s1+1,s2+1); sam.clear(); for (ri i=1,l=strlen(s1+1);i<=l;i++) sam.extend(s1[i]-‘a‘); printf("%d\n",sam.match()); return 0; }
标签:eof style clu lin ace har lcs poj class
原文地址:https://www.cnblogs.com/shxnb666/p/11279167.html