Description
A string is finite sequence of characters over a non-empty finite set Σ.
In this problem, Σ 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 a bit harder, for some 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 at most 10 lines, each line consists of no more than 100000 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
aaaajfaaaa Output:
2
题意:给若干个字符串,长度不超过100000,求最长公共连续字串
解析:后缀自动机,对输入的第一个字符串建一个后缀自动机,在
sam中添加两个量Max[i](查询时以第i个节点为最后一个字符的后缀的最大长度)
,Min[i](所有查询Max[i]中的最小值)。
代码
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=;
struct SAM
{
int ch[maxn][];
int pre[maxn],step[maxn];
int last,id;
int Min[maxn],Max[maxn];
void init() //初始化
{
last=id=;
memset(ch[],-,sizeof(ch[]));
pre[]=-; step[]=;
Min[]=Max[]=;
}
void Insert(int c) //模板,自己百度
{
int p=last,np=++id;
step[np]=step[p]+;
memset(ch[np],-,sizeof(ch[np]));
Min[np]=Max[np]=step[np];
while(p!=-&&ch[p][c]==-) ch[p][c]=np,p=pre[p];
if(p==-) pre[np]=;
else
{
int q=ch[p][c];
if(step[q]!=step[p]+)
{
int nq=++id;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
step[nq]=step[p]+;
Min[nq]=Max[nq]=step[nq];
pre[nq]=pre[q];
pre[np]=pre[q]=nq;
while(p!=-&&ch[p][c]==q) ch[p][c]=nq,p=pre[p];
}
else pre[np]=q;
}
last=np;
}
void Find(char *S)
{
int len=strlen(S);
int u=,d=;
for(int i=;i<=id;i++) Max[i]=;
for(int i=;i<len;i++)
{
int c=S[i]-'a';
if(ch[u][c]!=-) d++,u=ch[u][c];
else
{
while(u!=-&&ch[u][c]==-) u=pre[u];
if(u!=-) d=step[u]+,u=ch[u][c];
else u=,d=;
}
Max[u]=max(Max[u],d);//更新
}
for(int i=id;i>=;i--) Max[pre[i]]=max(Max[pre[i]],Max[i]);
for(int i=;i<=id;i++) Min[i]=min(Min[i],Max[i]);
}
int GetAns()
{
int ret=;
for(int i=;i<=id;i++) ret=max(ret,Min[i]);
return ret;
}
}sam;
char S[maxn];
int main()
{
scanf("%s",S);
int len=strlen(S);
sam.init();
for(int i=;i<len;i++) sam.Insert(S[i]-'a');
while(scanf("%s",S)!=EOF) sam.Find(S);
printf("%d\n",sam.GetAns());
return ;
}