Longest Common Substring SPOJ - LCS (后缀自动机)

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 simple, for two 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 exactly two lines, each line consists of no more than 250000 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

Output:
3

Notice: new testcases added

题目大意:求两个串的最长公共子串。

解题思路:对第一个字符串建立后缀自动机(SAM), 然后让第二个字符串在SAM上跑就可以了。

/*
@Author: Top_Spirit
@language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
typedef pair < int, int > P ;
const int Maxn = 25e4 + 10 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const ull seed = 133 ;

string s1, s2 ;
int len1, len2 ;

struct SAM{
    int son[Maxn << 1][26],step[Maxn << 1],pre[Maxn << 1];
    int tot,root,last;
    void add(int ch, int id){
        int p = last, np = ++tot ;
        last = np ;
//        step[np] = step[last] + 1 ;
        step[np] = id ;
        for (; p && !son[p][ch]; p = pre[p]) son[p][ch] = np ;
        if (!p) pre[np] = root ;
        else {
            int q = son[p][ch] ;
            if (step[q] != step[p] + 1) {
                int nq = ++tot ;
                step[nq] = step[p] + 1 ;
                memcpy(son[nq], son[q], sizeof(son[q])) ;
                pre[nq] = pre[q] ;
                pre[np] = pre[q] = nq ;
                for ( ; son[p][ch] == q; p = pre[p]) son[p][ch] = nq ;
            }
            else pre[np] = q ;
        }
    }
    void build(string s){
        for (int i = 0; i < len1; i++){
            int id = s[i] - 'a' ;
            add(id, i) ;
        }
    }
    void init(){
//        memset(son, 0, sizeof(son)) ;
//        memset(pre, 0, sizeof(pre)) ;
//        memset(step, 0, sizeof(step)) ;
        last = root = ++tot ;
    }
}sa;

int main (){
    cin >> s1 >> s2 ;
    len1 = s1.size() ;
    len2 = s2.size() ;
    sa.init() ;
    sa.build(s1) ;
    int ans = 0, len = 0, p = sa.root ;
    for (int i = 0; i < len2; i++){
        int id = s2[i] - 'a' ;
        if (sa.son[p][id]) {
            len++ ;
            p = sa.son[p][id] ;
        }
        else {
            while (p && !sa.son[p][id]) p = sa.pre[p] ;
            if (!p) p = sa.root, len = 0 ;
            else {
                len = sa.step[p] + 1 ;
                p = sa.son[p][id] ;
            }
        }
        ans = max(ans, len) ;
    }
    cout << ans << endl ;
    return 0 ;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值