1. 程式人生 > >spoj LCS2(多個串的最長公共子序列,字尾自動機)

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 );
}