SPOJ LCS2 - Longest Common Substring II 后缀自动机 多个串的LCS

时间:2023-03-08 16:29:33

LCS2 - Longest Common Substring II

no tags 

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

Notice: new testcases added

题意:

  求多个串的最长公共字串

题解:

  将第一个串建立后缀自动机

  根据拓扑排序更新每个状态节点下所能向前延伸的长度,每次匹配一个串都更新即可

  是不是很模糊

#include <bits/stdc++.h>
inline long long read(){long long x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}
using namespace std; const int N = 3e5+; const long long mod = ; int isPlus[N * ],endpos[N * ];int d[N * ];
int tot,slink[*N],trans[*N][],minlen[*N],maxlen[*N],pre;
int newstate(int _maxlen,int _minlen,int* _trans,int _slink){
maxlen[++tot]=_maxlen;minlen[tot]=_minlen;
slink[tot]=_slink;
if(_trans)for(int i=;i<;i++)trans[tot][i]=_trans[i],d[_trans[i]]+=;
return tot;
}
int add_char(char ch,int u){
int c=ch-'a',v=u;
int z=newstate(maxlen[u]+,-,NULL,);
isPlus[z] = ;
while(v&&!trans[v][c]){trans[v][c]=z;d[z]+=;v=slink[v];}
if(!v){ minlen[z]=;slink[z]=;return z;}
int x=trans[v][c];
if(maxlen[v]+==maxlen[x]){slink[z]=x;minlen[z]=maxlen[x]+;return z;}
int y=newstate(maxlen[v]+,-,trans[x],slink[x]);
slink[z]=slink[x]=y;minlen[x]=minlen[z]=maxlen[y]+;
while(v&&trans[v][c]==x){trans[v][c]=y;d[x]--,d[y]++;v=slink[v];}
minlen[y]=maxlen[slink[y]]+;
return z;
}
void init_sam() {
for(int i = ; i <= tot; ++i)
for(int j = ; j < ; ++j) trans[i][j] = ;
pre = tot = ;
} int dp[N],ans,n,vis[N],cnt[N],pos[N];
char a[N];
int main() {
int f = ;
while(scanf("%s",a)!=EOF) {
n = strlen(a);
if(f) {
init_sam();
for(int i = ; i < n; ++i)
pre = add_char(a[i],pre);
f = ;
for(int i = ; i <= n; ++i)cnt[i] = ;
for(int i = ; i <= tot; ++i)cnt[maxlen[i]] += ;
for(int i = ; i <= n; ++i)cnt[i]+=cnt[i-];
for(int i = tot; i >= ; --i)pos[cnt[maxlen[i]]--] = i;
}
else
{
int now = ,sum = ;
for(int i = ; i < n; ++i)
{
if(trans[now][a[i]-'a']) {
sum++;
now = trans[now][a[i]-'a'];
dp[now] = max(dp[now],sum);
}
else {
while(now) {
now = slink[now];
if(trans[now][a[i]-'a']) break;
}
if(!now) now = ,sum = ;
else
{
sum = maxlen[now] + ;
now = trans[now][a[i]-'a'];
dp[now] = max(dp[now],sum);
}
}
}
for(int i = tot; i >= ; --i) {
int v = pos[i];
maxlen[v] = min(maxlen[v],dp[v]);
dp[slink[v]] = max(dp[slink[v]],dp[v]);
dp[v] = ;
}
}
}
int ans = ;
for(int i = ; i <= tot; ++i) {
ans = max(ans,maxlen[i]);
}
printf("%d\n",ans);
return ;
}
/*
aabbcd
babbefg
cdbbabb
*/