SPOJ LCS (SAM)

7 篇文章 0 订阅

Description

求两个串的lcs

Solution

建出一个串的SAM,然后另一个串在上面匹配即可。

Source

/************************************************
 * Au: Hany01
 * Date: Apr 1st, 2018
 * Prob: SPOJ LCS
 * Email: hany01@foxmail.com
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 250005;

int n1, n2, fa[maxn << 1], ch[maxn << 1][26], len[maxn << 1], tot = 1, las = 1;
char s1[maxn], s2[maxn];

inline void extend(short c)
{
    int np = ++ tot, p = las;
    las = np, len[np] = len[p] + 1;
    while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
    if (!p) fa[np] = 1;
    else {
        int q = ch[p][c];
        if (len[q] == len[p] + 1) fa[np] = q;
        else {
            int nq = ++ tot;
            fa[nq] = fa[q], Cpy(ch[nq], ch[q]);
            len[nq] = len[p] + 1;
            fa[np] = fa[q] = nq;
            while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
        }
    }
}

int main()
{
#ifdef hany01
    File("LCS");
#endif

    //Init
    scanf("%s%s", s1 + 1, s2 + 1);
    n1 = strlen(s1 + 1), n2 = strlen(s2 + 1);
    For(i, 1, n1) extend(s1[i] - 97);

    //Solve
    int j = 1, now = 0, Ans = 0;
    For(i, 1, n2)
    {
        register short c = s2[i] - 97;
        if (ch[j][c]) ++ now, j = ch[j][c];
        else {
            while (j && !ch[j][c]) j = fa[j];
            if (!j) now = 0, j = 1; else now = len[j] + 1, j = ch[j][c];
        }
        chkmax(Ans, now);
    }
    printf("%d\n", Ans);

    return 0;
}
//生命的意义在于付出, 在于给予,
//而不是在于接受,也不是在于争取。
//  --巴金
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值