[SPOJ1812]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

Solution
把一个串建成自动机,其它的在上面跑,统计出每个串在每个状态的最大匹配长度,每个状态取所有串的最小值得到所有串在该状态的最大匹配长度,最后扫描所有状态取最大值得到结果

Code

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

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define MS(_) memset(_, 0, sizeof(_))
#define MP make_pair
#define PB push_back
inline void ckmax(int &x, int y){ if (x < y) x = y; }
inline void ckmin(int &x, int y){ if (x > y) x = y; }

const int INF = 0x7fffffff;
const int N = 200100;
char s[N];
int len[N], nxt[N][26], fa[N], d[N], t[N], f[N], mn[N];

struct sam{
    int root, last, cnt, LEN;
    sam() { cnt = 0; root = last = ++cnt; }
    inline void insert(int c){
        int np = ++cnt, p = last; last = np;
        mn[np] = len[np] = len[p]+1;
        for (; p && !nxt[p][c]; nxt[p][c] = np, p = fa[p]);
        if (!p) fa[np] = root;
        else if (len[nxt[p][c]] == len[p]+1) fa[np] = nxt[p][c];
        else{
            int nq = ++cnt, q = nxt[p][c]; mn[nq] = len[nq] = len[p]+1;
            memcpy(nxt[nq], nxt[q], sizeof nxt[q]); fa[nq] = fa[q];
            fa[q] = fa[np] = nq;
            for (; p && nxt[p][c] == q; nxt[p][c] = nq, p = fa[p]);
        }
    }
    inline void build(){ char s[N]; 
        scanf("%s", s+1); LEN = strlen(s+1);
        rep(i, 1, LEN) insert(s[i]-'a');
    }
    inline void topsort(){
        rep(i, 1, cnt) d[len[i]]++;
        rep(i, 1, LEN) d[i] += d[i-1];
        rep(i, 1, cnt) t[d[len[i]]--] = i;
    }
    inline bool run(){
        if (scanf("%s", s+1) == EOF) return false;
        MS(f); int _len = strlen(s+1), nowlen = 0;
        for (int i = 1, p = root; i <= _len; i++){ int c = s[i]-'a';
            if (nxt[p][c]) { nowlen++; p = nxt[p][c]; }
            else{
                for (; p && !nxt[p][c]; p = fa[p]);
                if (!p) { p = root; nowlen = 0; }
                else { nowlen = len[p] + 1; p = nxt[p][c]; }
            }
            ckmax(f[p], nowlen);        
        }
        per(i, cnt, 1){ int now = t[i];
            ckmin(mn[now], f[now]); 
            //if (f[now]&&fa[now]) f[fa[now]] = len[fa[now]]; 
            ckmax(f[fa[now]], f[now]);
        }
        return true;
    }
}SAM;

int main(){
    SAM.build();
    SAM.topsort();
    while (SAM.run());
    int ans = 0;
    rep(i, 1, SAM.cnt) ckmax(ans, mn[i]);
    printf("%d\n", ans);
    return 0;
} 

你可能感兴趣的:(后缀自动机)