ADAPHOTO - Ada and Terramorphing 题解

题目链接:Spoj 或者 洛谷

挺水的经典题,关于 \(LCP\) 问题个人比较喜欢 \(SA\)。我们将两个匹配串用一个不曾出现的字符拼成一整个串,然后跑 \(SA\) 求出一些基本信息。对于 \(LCP\) 答案,一定是两个相邻的 \(sa\) 数组中取到。因为我们知道 \(sa\) 数组表示的后缀是单调的,所以最佳 \(LCP\) 一定是出现在某两个后缀满足一个后缀起点在前半部分串,另一个起点在后半部分串上。由于我们增加了一个特殊字符,使得 \(LCP\) 不会越过这个特殊字符,我们再处理一遍 \(height\) 数组,拿到任意两个相邻后缀的 \(LCP\) 就行了。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

// #define isPbdsFile

#ifdef isPbdsFile

#include <bits/extc++.h>

#else

#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>

#endif

using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};

template <typename T>
int disc(T* a, int n)
{
    return unique(a + 1, a + n + 1) - (a + 1);
}

template <typename T>
T lowBit(T x)
{
    return x & -x;
}

template <typename T>
T Rand(T l, T r)
{
    static mt19937 Rand(time(nullptr));
    uniform_int_distribution<T> dis(l, r);
    return dis(Rand);
}

template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
    return (a % b + b) % b;
}

template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
    a %= c;
    T1 ans = 1;
    for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
    return modt(ans, c);
}

template <typename T>
void read(T& x)
{
    x = 0;
    T sign = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')sign = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= sign;
}

template <typename T, typename... U>
void read(T& x, U&... y)
{
    read(x);
    read(y...);
}

template <typename T>
void write(T x)
{
    if (typeid(x) == typeid(char))return;
    if (x < 0)x = -x, putchar('-');
    if (x > 9)write(x / 10);
    putchar(x % 10 ^ 48);
}

template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
    write(x), putchar(c);
    write(c, y...);
}


template <typename T11, typename T22, typename T33>
struct T3
{
    T11 one;
    T22 tow;
    T33 three;

    bool operator<(const T3 other) const
    {
        if (one == other.one)
        {
            if (tow == other.tow)return three < other.three;
            return tow < other.tow;
        }
        return one < other.one;
    }

    T3() { one = tow = three = 0; }

    T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
    {
    }
};

template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
    if (x < y)x = y;
}

template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
    if (x > y)x = y;
}

namespace SuffixArray
{
    constexpr int N = 2e6 + 10; //数组大小
    constexpr int BUKSize = 128; //初始桶的个数
    int sa[N], rnk[N], h[N]; //后缀数组
    int s[N]; //字符串
    int n; //字符串长
    //两个类型
    enum STR
    {
        L,
        S
    };

    //判断是否是LMS,当前位为S型,左边一位为L型
    bool isLMS(auto type, int pos)
    {
        return !(pos == 0 || type[pos] != S || type[pos - 1] != L);
    }

    //判断是否是相等的LMS
    bool isSameLMS(auto str, auto type, const int i, const int j)
    {
        if (i == -1 || j == -1)return false;
        int k = -1;
        while (true)
        {
            ++k;
            if (str[i + k] != str[j + k] || type[i + k] != type[j + k])return false; //如果对应字符不等,或者类型不等
            if (k == 0)continue;
            if (isLMS(type, i + k) != isLMS(type, j + k))return false;
            if (isLMS(type, i + k))return true;
        }
    }

    //诱导排序LLLLLSSSSSS
    void inSort(auto str, auto len, auto type, auto LMS, auto lmsSize, const int currBukSize)
    {
        auto buck = new int[currBukSize]; //桶
        auto pre = new int[currBukSize]; //桶前缀和
        memset(buck, 0, sizeof(buck[0]) * currBukSize);
        memset(pre, 0, sizeof(pre[0]) * currBukSize);
        memset(sa, -1, sizeof(int) * len);
        forn(i, 0, len - 1)++buck[str[i]]; //字符分桶
        pre[0] = buck[0];
        forn(i, 1, currBukSize - 1)pre[i] = pre[i - 1] + buck[i]; //前缀和
        //先处理LMS(特殊的S型)到S桶里
        forv(i, lmsSize - 1, 0)sa[--pre[str[LMS[i]]]] = LMS[i];
        //正逆序扫,分S型和L型两种桶进行排序SSSSSSLLLLLLLL
        //顺序处理L型
        pre[0] = 0;
        forn(i, 1, currBukSize - 1)pre[i] = pre[i - 1] + buck[i - 1];
        forn(i, 0, len - 1)if (sa[i] > 0 && type[sa[i] - 1] == L)sa[pre[str[sa[i] - 1]]++] = sa[i] - 1;
        //逆序处理S型
        pre[0] = buck[0];
        forn(i, 1, currBukSize - 1)pre[i] = pre[i - 1] + buck[i]; //前缀和
        forv(i, len - 1, 0)if (sa[i] > 0 && type[sa[i] - 1] == S)sa[--pre[str[sa[i] - 1]]] = sa[i] - 1;
        delete[] buck;
        delete[] pre;
    }

    //SA-IS
    void SA_IS(auto str, int len, auto LMS, int currBukSize)
    {
        auto type = new STR[N + 1];
        //默认最后增加一个字符'#'比任何字符小,所以最后一个字符为S型
        //处理类型
        //前后相等用性质否则用定义
        type[len - 1] = S;
        forv(i, len - 2, 0)type[i] = str[i] == str[i + 1] ? type[i + 1] : str[i] < str[i + 1] ? S : L;
        int lmsSize = 0;
        forn(i, 0, len - 1)if (isLMS(type, i))LMS[lmsSize++] = i;
        inSort(str, len, type, LMS, lmsSize, currBukSize); //诱导排序S型和L型
        int pre = -1; //上一个sa[i]
        int cnt = 0;
        auto tmp = new int[len]; //临时存值数组
        //记录还无法确定次序的LMS数组,还未确定次序就递归排序
        forn(i, 0, len - 1)
        {
            if (!isLMS(type, sa[i]))continue;
            if (!isSameLMS(str, type, pre, sa[i]))++cnt;
            tmp[sa[i]] = cnt - 1;
            pre = sa[i];
        }
        auto nxtStr = new int[lmsSize];
        lmsSize = 0;
        //记录LMS
        forn(i, 0, len - 1)
        {
            if (!isLMS(type, i))continue;
            nxtStr[lmsSize++] = tmp[i];
        }
        delete[] tmp;
        auto nxtLMS = new int[lmsSize];
        if (cnt < lmsSize)SA_IS(nxtStr, lmsSize, nxtLMS, cnt);
        else
            forn(i, 0, lmsSize - 1)sa[nxtStr[i]] = i;
        delete[] nxtStr;
        forn(i, 0, lmsSize - 1)nxtLMS[i] = LMS[sa[i]];
        inSort(str, len, type, nxtLMS, lmsSize, currBukSize);
        delete[] nxtLMS;
        delete[] type;
    }

    inline void init(const int len)
    {
        n = len;
        auto LMS = new int[n];
        //加一个字符'#'
        SA_IS(s, n + 1, LMS, BUKSize);
        delete[] LMS;
        forn(i, 0, n - 1)sa[i] = sa[i + 1] + 1;
        forv(i, n - 1, 0)swap(sa[i], sa[i + 1]), swap(s[i], s[i + 1]);
        sa[0] = sa[n + 1] = 0;
    }

    void get_h()
    {
        forn(i, 1, n)rnk[sa[i]] = i;
        for (int i = 1, k = 0; i <= n; i++)
        {
            if (rnk[i] == 1)continue;
            if (k)k--; //大于等于上一个后缀的h-1
            int ear = sa[rnk[i] - 1]; //上一个后缀
            while (i + k <= n && ear + k <= n && s[i + k] == s[ear + k])k++;
            h[rnk[i]] = k;
        }
    }

    inline void clear()
    {
        forn(i, 0, n)sa[i] = rnk[i] = h[i] = 0;
        forn(i, 0, n)s[i] = '\0';
    }
}

using namespace SuffixArray;
string s1, s2;
int idx;
hash2<char, int> mp;

inline void solve()
{
    mp['-'] = 1, mp['^'] = 2, mp['~'] = 3, mp['v'] = 4;
    cin >> s1 >> s2;
    const int split = s1.length();
    forn(i, 0, s1.length()-1)s[idx++] = mp[s1[i]];
    s[idx++] = 5;
    forn(i, 0, s2.length()-1)s[idx++] = mp[s2[i]];
    init(idx);
    get_h();
    int ans = 0;
    forn(i, 2, idx)if (sa[i] < split != sa[i - 1] < split)uMax(ans, h[i]);
    cout << ans;
}

signed int main()
{
    // MyFile
    Spider
    //------------------------------------------------------
    // clock_t start = clock();
    int test = 1;
    //    read(test);
    // cin >> test;
    forn(i, 1, test)solve();
    //    while (cin >> n, n)solve();
    //    while (cin >> test)solve();
    // clock_t end = clock();
    // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}

\[最终使用\ SA-IS\ 建后缀数组,时间复杂度为:\ O(n) \]

posted @ 2024-02-29 14:42  Athanasy  阅读(12)  评论(0编辑  收藏  举报