1. 程式人生 > >LCS2 Longest Common Substring II (求若干個串的最長公共子串模板題)

LCS2 Longest Common Substring II (求若干個串的最長公共子串模板題)

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
#include<cstdio>
#include<cstring>

using namespace std;

const int MAXN = 3*1e5+10;
//串的長度最大為1e5但空間儘量大點。 

char s[MAXN];
int sz,last,root,l[MAXN],ch[MAXN][26],fa[MAXN],mn[MAXN],mx[MAXN];
int b[MAXN],cnt[MAXN];

void add(int x) {
    int c = s[x]-'a';//如果串由大寫字母組成改這裡 
    int p=last,np=++sz; last=np; 
    l[np]=mn[np]=x+1;
    for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
    if(!p) fa[np]=root;
    else {
        int q=ch[p][c];
        if(l[p]+1==l[q]) fa[np]=q;
        else {
            int nq=++sz; l[nq]=mn[nq]=l[p]+1;
            memcpy(ch[nq],ch[q],sizeof(ch[q]));
            fa[nq]=fa[q];
            fa[np]=fa[q]=nq;
            for(;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
        }
    }
}

int main() {
    root = last = ++sz;
    scanf("%s",s);//先讀第一串 
    int len=strlen(s);
    for(int i=0;i<len;i++) add(i);
    for(int i=1;i<=sz;i++) cnt[l[i]]++;
    for(int i=1;i<=len;i++) cnt[i]+=cnt[i-1];
    for(int i=1;i<=sz;i++) b[cnt[l[i]]--]=i;
    
    while(scanf("%s",s) == 1) {
        int p=root; len=0;
        for(int i=0 ; s[i] ; ++i) {
            int c = s[i]-'a';//如果串由大寫字母組成改這裡
            if(ch[p][c]) { len++; p=ch[p][c]; }
            else {
                while(p&&!ch[p][c]) p=fa[p];
                if(!p) { len=0; p=root; }
                else { len=l[p]+1; p=ch[p][c]; }
            }
            if(len>mx[p]) mx[p]=len;
        }
        for(int i=sz ; i ; i--) {
            p=b[i];
            if(mx[p]<mn[p]) mn[p]=mx[p];
            if(fa[p] && mx[fa[p]]<mx[p]) mx[fa[p]]=mx[p];
            mx[p]=0;
        }
    }
    int ans=0;
    for(int i=1 ; i<=sz ; ++i)
        if(mn[i]>ans) ans=mn[i];
    printf("%d",ans);
    
    return 0;
}