[POJ 2774]Long Long Message

Posted navi-awson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[POJ 2774]Long Long Message相关的知识,希望对你有一定的参考价值。

Description

题库链接

给定两个字符串 (A)(B) ,求最长公共子串。

(1leq |A|,|B|leq 100000)

Solution

把串并起来求 (height_i) 的最大值,其中 ([suff(sa_{i-1})subseteq B]oplus [suff(sa_i)subseteq B]=1)

Code

#include <bits/stdc++.h>
using namespace std;
const int N = (100000+5)<<1;

char ch[N];
int s1, n, m, x[N<<1], y[N<<1], c[N], sa[N], rk[N], height[N];

void get() {
    for (int i = 1; i <= n; i++) c[x[i] = ch[i]]++;
    for (int i = 2; i <= m; i++) c[i] += c[i-1];
    for (int i = n; i >= 1; i--) sa[c[x[i]]--] = i;
    for (int k = 1; k <= n; k <<= 1) {
        int num = 0;
        for (int i = n-k+1; i <= n; i++) y[++num] = i;
        for (int i = 1; i <= n; i++) if (sa[i] > k) y[++num] = sa[i]-k;
        for (int i = 1; i <= m; i++) c[i] = 0;
        for (int i = 1; i <= n; i++) c[x[i]]++;
        for (int i = 2; i <= m; i++) c[i] += c[i-1];
        for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
        swap(x, y); x[sa[1]] = num = 1;
        for (int i = 2; i <= n; i++)
            x[sa[i]] = (y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]) ? num : ++num;
        if ((m = num) == n) break;
    }
    for (int i = 1; i <= n; i++) rk[sa[i]] = i;
    for (int i = 1, k = 0; i <= n; i++) {
        if (rk[i] == 1) continue;
        if (k) --k; int j = sa[rk[i]-1];
        while (i+k <= n && j+k <= n && ch[i+k] == ch[j+k]) ++k;
        height[rk[i]] = k;
    }
}
void work() {
    scanf("%s", ch+1); s1 = strlen(ch+1);
    ch[s1+1] = '$';
    scanf("%s", ch+s1+2); n = strlen(ch+1); m = 255;
    get(); int ans = 0;
    for (int i = 2; i <= n; i++)
        if (((sa[i-1] <= s1)^(sa[i] <= s1)) && !(sa[i-1] == s1+1) && !(sa[i] == s1+1))
            ans = max(ans, height[i]);
    printf("%d
", ans);
}
int main() {work(); return 0; }

以上是关于[POJ 2774]Long Long Message的主要内容,如果未能解决你的问题,请参考以下文章

poj2774 Long Long Message

POJ 2774 Long Long Message(后缀数组[最长公共子串])

POJ 2774 Long Long Message 后缀数组

POJ 2774 Long Long Message(后缀数组[最长公共子串])

POJ2774 Long Long Message [后缀数组]

Long Long Message POJ - 2774