SP1812 LCS2 - Longest Common Substring II

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SP1812 LCS2 - Longest Common Substring II相关的知识,希望对你有一定的参考价值。

???????????????   ??????   ==   ??????   ??????   bst   ??????   als   match   

(color{#0066ff}{ ???????????? })

???????????? ????????????????????????????????????????????????????????? ???????????? ????????????(10) ???????????????????????????(100000) ?????????????????????????????????????????? ???????????? ??????????????????????????????????????? ??????????????????????????????????????????(0) ???

(color{#0066ff}{????????????})

???????????????

(color{#0066ff}{????????????})

?????????????????? ????????????

(color{#0066ff}{????????????})

alsdfkjfjkdsal
fdjskalajfkdsla
aaaajfaaaa

(color{#0066ff}{????????????})

2

(color{#0066ff}{?????????????????????})

none

(color{#0066ff}{ ?????? })

???????????????????????????SAM

?????????????????????max???min ?????????????????????????????????????????????len??? ?????????????????????len

?????????????????????????????????????????????

????????????????????????max???0??? ???????????????????????????????????????len??????????????????max

??????????????????????????????????????????????????????????????????????????????????????????

????????????max??????min????????????len??? ?????????max??????max

???????????????????????????????????????????????????????????????????????????????????????????????????????????????len???min?????????????????????max???max?????????

????????????????????????max??????ans???

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
    char ch; int x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
const int maxn = 2e5 + 5;
const int inf = 0x7f7f7f7f;
struct SAM {
protected:
    struct node {
        node *ch[26], *fa;
        int len, siz, max, min;
        node(int len = 0, int siz = 0, int max = 0, int min = inf): fa(NULL), len(len), siz(siz), max(max), min(min) {
            memset(ch, 0, sizeof ch);
        }
    };
    node *root, *tail, *lst;
    node pool[maxn], *id[maxn];
    int c[maxn];
    void extend(int c) {
        node *o = new(tail++) node(lst->len + 1, 1), *v = lst;
        for(; v && !v->ch[c]; v = v->fa) v->ch[c] = o;
        if(!v) o->fa = root;
        else if(v->len + 1 == v->ch[c]->len) o->fa = v->ch[c];
        else {
            node *n = new(tail++) node(v->len + 1), *d = v->ch[c];
            std::copy(d->ch, d->ch + 26, n->ch);
            n->fa = d->fa, d->fa = o->fa = n;
            for(; v && v->ch[c] == d; v = v->fa) v->ch[c] = n;
        }
        lst = o;
    }
    void clr() {
        tail = pool;
        root = lst = new(tail++) node();
    }
public:
    SAM() { clr(); }
    void ins(char *s) { for(char *p = s; *p; p++) extend(*p - 'a'); }
    void getid() {
        int maxlen = 0;
        for(node *o = pool; o != tail; o++) c[o->len]++, maxlen = std::max(maxlen, o->len);
        for(int i = 1; i <= maxlen; i++) c[i] += c[i - 1];
        for(node *o = pool; o != tail; o++) id[--c[o->len]] = o;
    }
    void match(char *s) {
        node *o = root;
        int len = 0;
        for(char *p = s; *p; p++) {
            int pos = *p - 'a';
            if(o->ch[pos]) o = o->ch[pos], len++;
            else {
                while(o && !o->ch[pos]) o = o->fa;
                if(!o) o = root, len = 0;
                else len = o->len + 1, o = o->ch[pos];
            }
            o->max = std::max(o->max, len);
        }
        for(int i = tail - pool - 1; i; i--) {
            node *o = id[i];
            if(o->fa) o->fa->max = std::max(o->fa->max, std::min(o->max, o->fa->len));
            o->min = std::min(o->min, o->max);
            o->max = 0;
        }
    }
    int getans() {
        int ans = 0;
        for(int i = tail - pool - 1; i; i--) ans = std::max(ans, id[i]->min);
        return ans;
    }
}sam;
char s[maxn];
int main() {
    scanf("%s", s);
    sam.ins(s);
    sam.getid();
    while(~scanf("%s", s)) sam.match(s);
    printf("%d
", sam.getans());
    return 0;
}

以上是关于SP1812 LCS2 - Longest Common Substring II的主要内容,如果未能解决你的问题,请参考以下文章

SP1812 LCS2 - Longest Common Substring II

spoj1812 LCS2 - Longest Common Substring II

刷题SPOJ 1812 LCS2 - Longest Common Substring II

spoj 1812 LCS2 - Longest Common Substring II (后缀自己主动机)

[SPOJ1812-LCS2]Longest Common Substring II

SPOJ 1812 LCS2 - Longest Common Substring II (后缀自动机)两种做法