后缀数组

2024-08-15

通过基数排序构建后缀数组,进而求解最长公共字串问题

#include <bits/stdc++.h>
#define io cin.tie(0), cout.tie(0), ios::sync_with_stdio(false)
#define LL long long
#define ULL unsigned long long
#define EPS 1e-8
#define INF 0x7fffffff
#define SUB -INF - 1
using namespace std;
const int N = 1000005;
char s[N];
int sa[N], cnt[N], t1[N], t2[N], rk[N], height[N];
int n;
void cacl_sa()
{
    int m = 127;
    int i, *x = t1, *y = t2;
    for (i = 0; i < m; i++)
        cnt[i] = 0;
    for (i = 0; i < n; i++)
        cnt[x[i] = s[i]]++;
    for (i = 1; i < m; i++)
        cnt[i] += cnt[i - 1];
    for (i = n - 1; i >= 0; i--)
        sa[--cnt[x[i]]] = i;
    for (int k = 1; k <= n; k *= 2)
    {
        int p = 0;
        for (i = n - k; i < n; i++)
            y[p++] = i;
        for (i = 0; i < n; i++)
            if (sa[i] >= k)
                y[p++] = sa[i] - k;
        for (i = 0; i < m; i++)
            cnt[i] = 0;
        for (i = 0; i < n; i++)
            cnt[x[y[i]]]++;
        for (i = 1; i < m; i++)
            cnt[i] += cnt[i - 1];
        for (i = n - 1; i >= 0; i--)
            sa[--cnt[x[y[i]]]] = y[i];
        swap(x, y);
        p = 1;
        x[sa[0]] = 0;
        for (i = 1; i < n; i++)
            x[sa[i]] = (y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]) ? (p - 1) : p++;
        if (p >= n)
            break;
        m = p;
    }
}
void getheight(int n)
{
    int i, k = 0;
    for (i = 0; i < n; i++)
        rk[sa[i]] = i;
    for (i = 0; i < n; i++)
    {
        if (k)
            k--;
        int j = sa[rk[i] - 1];
        while (s[i + k] == s[j + k])
            k++;
        height[rk[i]] = k;
    }
}
int main()
{
    int len1, ans;
    while (scanf("%s", s) != EOF)
    {
        n = strlen(s);
        len1 = n;
        s[n] = '$';
        scanf("%s", s + n + 1);
        n = strlen(s);
        cacl_sa();
        getheight(n);
        ans = 0;
        for (int i = 1; i < n; i++)
            if (height[i] > ans && ((sa[i - 1] < len1 && sa[i] >= len1) || (sa[i - 1] >= len1 && sa[i] < len1)))
                ans = height[i];
        printf("%d\n", ans);
    }
    return 0;
}