POJ - 2774 Long Long Message

It is intended a topic: seek length set to the longest common string two strings.

We suffix array \ (H \) array obviously has this property. Two strings will fit together, note that the middle of a symbol will not appear, as may be required to span \ (A \) strings \ (B \) string "substring" will emerge. (But without the pro-test can \ (A \) )

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 200002;

int n, m, h[N], tax[N], SA[N], tp[N], rk[N], a[N];

int read() {
    int x = 0, f = 1; char S;
    while((S = getchar()) > '9' || S < '0') {
        if(S == '-') f = -1;
        if(S == EOF) exit(0);
    }
    while(S <= '9' && S >= '0') {
        x = (x << 1) + (x << 3) + (S ^ 48);
        S = getchar();
    }
    return x * f;
}

bool cmp(const int x, const int y, const int d) {return tp[x] == tp[y] && tp[x + d] == tp[y + d];}

void Sort() {
    for(int i = 0; i <= m; ++ i) tax[i] = 0;
    for(int i = 1; i <= n; ++ i) ++ tax[rk[tp[i]]];
    for(int i = 1; i <= m; ++ i) tax[i] += tax[i - 1];
    for(int i = n; i >= 1; -- i) SA[tax[rk[tp[i]]] --] = tp[i];
}

void Suffix() {
    for(int i = 1; i <= n; ++ i) rk[i] = a[i], tp[i] = i;
    m = 122; Sort();
    for(int w = 1, p = 1, i; p < n; m = p, w <<= 1) {
        for(p = 0, i = n - w + 1; i <= n; ++ i) tp[++ p] = i;
        for(i = 1; i <= n; ++ i) if(SA[i] > w) tp[++ p] = SA[i] - w;
        Sort(); swap(rk, tp); rk[SA[1]] = p = 1;
        for(i = 2; i <= n; ++ i) rk[SA[i]] = cmp(SA[i], SA[i - 1], w) ? p : ++ p;
    }
    int j, k = 0;
    for(int i = 1; i <= n; h[rk[i ++]] = k)
        for(k = k ? k - 1 : k, j = SA[rk[i] - 1]; a[i + k] == a[j + k]; ++ k);
}

int main() {
    char ch[N];
    int ans = 0, tmp;
    scanf("%s", ch); n = strlen(ch);
    for(int i = 0; i < n; ++ i) a[i + 1] = ch[i];
    scanf("%s", ch);
    tmp = n;
    a[n + 1] = '#'; ++ n;
    for(int i = n + 1, siz = strlen(ch); i <= n + siz; ++ i) a[i] = ch[i - n - 1];
    n += strlen(ch); Suffix();
    for(int i = 2; i <= n; ++ i)
        if(1ll * (SA[i] - tmp - 1) * (SA[i - 1] - tmp - 1) < 0) ans = max(ans, h[i]);
    printf("%d\n", ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/AWhiteWall/p/12383292.html