SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机)

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


题目大意:给出n个串,求n个串的最长公共子串。

题解:后缀自动机

这道题可以类比两个的情况来做。

与之不同的是我们需要对第一个串建立后缀自动机。然后对于之后的每个串在自动机上匹配,记录下来每个串到达每个状态(节点)匹配的长度,针对每次状态去最小值,然后从最小值中选取最大值即为答案。有一点需要注意的是匹配的时候并不是每一个是子串的位置都能匹配到,所以我们需要按照拓扑序进行进一步的更新。对于一个状态如果我们匹配到了,那么他fa的状态一定是可以匹配到的,那么我们也需要将cl[fa[t]]=l[fa[t]]。

#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>  
#include<cmath>  
#define N 200005  
using namespace std;  
int ch[N][30],fa[N],l[N],cl[N],mn[N],ans,a[N];  
int n,m,last,root,p,q,np,nq,cnt,v[N],pos[N];  
char s[N],s1[N];   
void extend(int x)  
{  
    int c=s[x]-'a';  
    p=last; np=++cnt; last=np;  
    l[np]=x;  
    for (;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;  
    if (!p) fa[np]=root;  
    else {  
        q=ch[p][c];  
        if (l[p]+1==l[q]) fa[np]=q;  
        else {  
            nq=++cnt; l[nq]=l[p]+1;  
            memcpy(ch[nq],ch[q],sizeof ch[nq]);  
            fa[nq]=fa[q];  
            fa[q]=fa[np]=nq;  
            for (;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;  
        }  
    }  
}  
void solve()  
{  
    int tmp=0;  p=root;
    memset(cl,0,sizeof(cl));  
    for (int i=1;i<=n;i++){  
        int c=s[i]-'a';  
        if (ch[p][c]) p=ch[p][c],tmp++;  
        else {  
            while (p&&!ch[p][c]) p=fa[p];  
            if (!p) p=1,tmp=0;  
            else tmp=l[p]+1,p=ch[p][c];  
        }  
        cl[p]=max(cl[p],tmp);  
    }  
    for (int i=cnt;i>=1;i--){  
      int t=pos[i];  
      mn[t]=min(mn[t],cl[t]);  
      if (cl[t]&&fa[t]) cl[fa[t]]=l[fa[t]];  
    }  
}  
int main()  
{  
    //freopen("a.in","r",stdin);
    int t=0;  
    memset(mn,127,sizeof(mn));  
    while (scanf("%s",s+1)!=EOF) {  
        t++; n=strlen(s+1);  
        if (t==1) {  
            root=last=++cnt;  
            for (int i=1;i<=n;i++) extend(i);  
            for (int i=1;i<=cnt;i++) v[l[i]]++;  
            for (int i=1;i<=n;i++) v[i]+=v[i-1];  
            for (int i=1;i<=cnt;i++) pos[v[l[i]]--]=i;  
        }  
        else solve();  
    }  
    for (int i=1;i<=cnt;i++) ans=max(ans,mn[i]);  
    printf("%d\n",ans);  
}   




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值