fastle
垆边人似月 皓腕凝霜雪
/*
最长公共子串
用后缀自动机实现
首先建立一个串的后缀自动机
然后用另一个串在上面跑自动机即可

*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define M 500010
using namespace std;
char s[M];
int ch[M][26], fa[M], len[M], lst = 1, cnt = 1;

void insert(int c) {
    int f = lst, p = ++cnt;
    len[p] = len[f] + 1;
    lst = p;
    while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
    if(!f) fa[p] = 1;
    else
    {
        int q = ch[f][c];
        if(len[q] == len[f] + 1) fa[p] = q;
        else
        {
            int nq = ++cnt;
            len[nq] = len[f] + 1;
            fa[nq] = fa[q];
            fa[q] = fa[p] = nq;
            memcpy(ch[nq], ch[q], sizeof(ch[nq]));
            while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
        }
    }
}

int main() {
    scanf("%s", s);
    int l = strlen(s);
    for(int i = 0; i < l; i++) insert(s[i] - 'a');
    scanf("%s", s);
    l = strlen(s);
    int ans = 0;
    int now = 1, tmp = 0;
    for(int i = 0; i < l; i++) {
        int op = s[i] - 'a';
        if(ch[now][op]) {
            now = ch[now][op];
            tmp++;
        } else {
            while(now && !ch[now][op]) now = fa[now];
            if(now == 0) tmp = 0,now = 1;
            else tmp = len[now] + 1, now = ch[now][op];
        }
        ans = max(ans, tmp);
    }
    cout << ans << "\n";
    return 0 ;
}

 

posted on 2018-06-29 21:55  fastle  阅读(149)  评论(0编辑  收藏  举报