spoj LCS2(多个串的最长公共子序列,后缀自动机)

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

多个串的最长公共子序列
和两个串的套路一样,每次记录能匹配到的点,能匹配到的点是一些串的集合,你能到这个点证明这个公共串必定是这个点的串集合里的串之一,那么问题来了。所以每个点记录一下Maxi,为这个点可以在每个串和A串匹配时可以接收的最长公共子串,同时还要往上更新fail 因为当前节点要是可以接受这个子串,那么fail结点必然也可以结束,而且接受的最大长度为max(Maxi[fail[p]],Maxi[p]),但是要和能到当前点的最大长度取min,统计每个串的时候都要对这个最大的取min,这样才是共有的最长子串。最后统计一遍答案就好

#include <bits/stdc++.h>
using namespace std;

const int maxn = 500000+5;
int last,tail,in[maxn],Min[maxn];
int Max[maxn],cnt[maxn],vis[maxn];
int nxt[maxn][26],fail[maxn];

char sa[maxn],sb[maxn];
int Maxi[maxn],Mini[maxn];


inline void build(char *s)
{
    while(*s)
    {
        int p=last,t=++tail,c=*s++-'a';
        Max[t]=Max[p]+1;
        Mini[t]=Max[t];
        Maxi[t]=0;
        while(p&&!nxt[p][c])
            nxt[p][c]=t,p=fail[p];
        if(p)
        {
            int q=nxt[p][c];
            if(Max[q]==Max[p]+1)
                fail[t]=q,Min[t]=Max[q]+1;
            else
            {
                int k=++tail;
                fail[k]=fail[q];
                fail[t]=fail[q]=k;
                Max[k]=Max[p]+1;
                Mini[k]=Max[k];
                Maxi[k]=0;
                memcpy(nxt[k],nxt[q],sizeof(nxt[q]));
                while(p&&nxt[p][c]==q)
                    nxt[p][c]=k,p=fail[p];
            }
        }
        else 
            fail[t]=Min[t]=1;
        last=t;
    }
}


int b[maxn];

int main()
{
    scanf("%s",sa);
    last=1,tail=1;
    build(sa);
    int hh=strlen(sa);
    for(int i=1;i<=tail;i++) cnt[Max[i]]++;
    for(int i=1;i<=hh;i++) cnt[i]+=cnt[i-1];
    for(int i=1;i<=tail;i++) b[cnt[Max[i]]--]=i;
    while(~scanf("%s",sb))
    {
        int h=strlen(sb);
        int p=1;
        int len=0;
        for(int i=0;i<h;i++)
        {
            int c=sb[i]-'a';
            if(nxt[p][c])
                len++,p=nxt[p][c];
            else{
                while(p&&!nxt[p][c])
                    p=fail[p];
                if(!p) p=1,len=0;
                else
                {
                    len=Max[p]+1;
                    p=nxt[p][c];
                }
            }
            Maxi[p]=max(Maxi[p],len);
        }
        for(int i=tail;i>=1;i--)
        {
            int p=b[i];
            Mini[p]=min(Mini[p],Maxi[p]);
            if(fail[p]) Maxi[fail[p]]=max(Maxi[p],Maxi[fail[p]]);
            Maxi[p]=0;
        }
    }

    int ans=0;
    for(int i=1;i<=tail;i++)
    {
        ans=max(ans,Mini[i]);
    }
    printf("%d",ans );
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值