题解 CF1326D2 【Prefix-Suffix Palindrome (Hard version)】

· · 题解

题意:
给出字符串 s ,找出最长的回文串 t ,满足:

1\leq |s|\leq 10^6

\mathcal O(n^2) 解法

找出最大的 p (1\leq p\leq \left\lfloor\dfrac{|s|}{2}\right\rfloor),使得 \text{pre}(s,p)=\text{rev}(\text{suf}(s,p)),则 p 一定为 a 的前缀,且 \text{rev}(p) 为 b 的后缀。
在剩余的子串 s\,[p+1,|s|-p] 中找出为回文串的最长前缀与最长后缀,选长度较大的那一个作为答案。

例如,s=abcdaaadacba,此时 p=3 ,在剩余的子串 daaada 中找出的为回文串的最长前缀为 daaad,最长后缀为 ada,由于找出的前缀长度较大,因此选 daaad 作为答案,可知 a=abcdaaad,b=cba,t=abcdaaadcba。

代码如下(快读模板):

#include <algorithm>
#include <cstring>
#include <string>
const int maxn = 5005;

char str[maxn];
bool ispalid(const std::string& str) // 字符串 str 是否为回文串
{
    std::string t = str;
    std::reverse(t.begin(), t.end());
    return t == str;
}
int main()
{
    int q;
    io.read(q);
    while (q--) {
        io.read(str + 1);
        int n = strlen(str + 1);
        std::string s(str + 1);
        int p = 0;
        for (int i = 1; i <= n / 2; i++)
            if (str[i] == str[n - i + 1])
                p = i;
            else
                break;
        int cnt1 = 0, cnt2 = 0;
        for (int i = p + 1; i <= n - p; i++) {
            if (ispalid(s.substr(p, i - p)))
                cnt1 = i;
            if (ispalid(s.substr(n - i, i - p)))
                cnt2 = i;
        }
        for (int i = 1; i <= p; i++)
            io.write(str[i]);
        if (cnt1 > cnt2)
            for (int i = p + 1; i <= cnt1; i++)
                io.write(str[i]);
        else
            for (int i = n - cnt2 + 1; i <= n - p; i++)
                io.write(str[i]);
        for (int i = n - p + 1; i <= n; i++)
            io.write(str[i]);
        writeln();
    }
    return 0;
}

\mathcal O(n) 解法

把 \mathcal O(n^2) 解法中判断前缀与后缀是否为回文串的朴素做法改为双模数哈希匹配即可。

代码如下(快读模板):

#include <algorithm>
#include <cstring>
const int maxn = 1000005, MOD1 = 1e9 + 7, MOD2 = 1e9 + 9, b1 = 31, b2 = 79;

int p1[maxn], p2[maxn], hash1[maxn], hash2[maxn], hash3[maxn], hash4[maxn], n;
char str[maxn];
bool issame(int l, int r, int x, int y) // 子串 l-r 是否与 rev(x-y) 相等
{
    x = n - x + 1, y = n - y + 1;
    std::swap(x, y);
    int x1 = (hash1[r] - 1LL * hash1[l - 1] * p1[r - l + 1] % MOD1 + MOD1) % MOD1;
    int x2 = (hash2[r] - 1LL * hash2[l - 1] * p2[r - l + 1] % MOD2 + MOD2) % MOD2;
    int x3 = (hash3[y] - 1LL * hash3[x - 1] * p1[y - x + 1] % MOD1 + MOD1) % MOD1;
    int x4 = (hash4[y] - 1LL * hash4[x - 1] * p2[y - x + 1] % MOD2 + MOD2) % MOD2;
    return x1 == x3 && x2 == x4;
}
bool ispalid(int l, int r) // 子串 l-r 是否为回文串
{
    int len = r - l + 1;
    return issame(l, l + (len - 1) / 2, l + len / 2, r);
}
int main()
{
    int q;
    io.read(q);
    p1[0] = p2[0] = 1;
    for (int i = 1; i <= 1000000; i++)
        p1[i] = 1LL * p1[i - 1] * b1 % MOD1, p2[i] = 1LL * p2[i - 1] * b2 % MOD2;
    while (q--) {
        io.read(str + 1);
        n = strlen(str + 1);
        for (int i = 1; i <= n; i++)
            hash1[i] = (1LL * hash1[i - 1] * b1 + str[i]) % MOD1;
        for (int i = 1; i <= n; i++)
            hash2[i] = (1LL * hash2[i - 1] * b2 + str[i]) % MOD2;
        for (int i = n; i >= 1; i--)
            hash3[n - i + 1] = (1LL * hash3[n - i + 1 - 1] * b1 + str[i]) % MOD1;
        for (int i = n; i >= 1; i--)
            hash4[n - i + 1] = (1LL * hash4[n - i + 1 - 1] * b2 + str[i]) % MOD2;
        int p = 0;
        for (int i = 1; i <= n / 2; i++)
            if (str[i] == str[n - i + 1])
                p = i;
            else
                break;
        int cnt1 = 0, cnt2 = 0;
        for (int i = p + 1; i <= n - p; i++) {
            if (ispalid(p + 1, i))
                cnt1 = i;
            if (ispalid(n - i + 1, n - p))
                cnt2 = i;
        }
        for (int i = 1; i <= p; i++)
            io.write(str[i]);
        if (cnt1 > cnt2)
            for (int i = p + 1; i <= cnt1; i++)
                io.write(str[i]);
        else
            for (int i = n - cnt2 + 1; i <= n - p; i++)
                io.write(str[i]);
        for (int i = n - p + 1; i <= n; i++)
            io.write(str[i]);
        writeln();
    }
    return 0;
}