LCS - Longest Common Substring(spoj1811) (sam+LCS)
阿新 • • 發佈:2018-12-10
A string is finite sequence of characters over a non-empty finite set \(\sum\).
In this problem, \(\sum\) is the set of lowercase letters.
Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.
Now your task is simple, for two given strings, find the length of the longest common substring of them.
Here common substring means a substring of two or more strings.
Input
The input contains exactly two lines, each line consists of no more than 250000 lowercase letters, representing a string.
Output
The length of the longest common substring. If such string doesn't exist, print "0" instead.
Example
Input:
alsdfkjfjkdsal
fdjskalajfkdsla
Output:
3
Notice: new testcases added
題意:
求兩個字串的最長公共子串
題解:
把第一個串建一個字尾自動機,然後把另一個串放在上面從根(1號節點)開始跑,如果失配,就往\(parent\)上跳,並把當前長度變為\(parent\)的\(len\);若匹配,把當前長度加一,並實時更新答案。
#include<bits/stdc++.h> using namespace std; const int N=2000010; char s[N]; int a[N],c[N]; struct SAM{ int last,cnt; int size[N],ch[N][26],fa[N<<1],l[N<<1]; void ins(int c){ int p=last,np=++cnt;last=np;l[np]=l[p]+1; for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np; if(!p)fa[np]=1; else{ int q=ch[p][c]; if(l[p]+1==l[q])fa[np]=q; else{ int nq=++cnt;l[nq]=l[p]+1; memcpy(ch[nq],ch[q],sizeof ch[q]); fa[nq]=fa[q];fa[q]=fa[np]=nq; for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq; } } size[np]=1; } void build(char s[]){ int len=strlen(s+1); last=cnt=1; for(int i=1;i<=len;++i)ins(s[i]-'a'); } void work(char s[]){ int len=strlen(s+1); int p=1,left=0,as=0,ans=0; while(left<=len){ left++; while(p&&(!ch[p][s[left]-'a']))p=fa[p],as=l[p]; if(!p)p=1,as=0; else{ as++; ans=max(ans,as); p=ch[p][s[left]-'a']; } } cout<<ans<<endl; } }sam; int main(){ cin>>s+1; sam.build(s); cin>>s+1; sam.work(s); }