一.题目链接

POJ-3974

二.题目大意:

求 s 的最大回文子串长度.

三.分析:

此题可以用 字符串 Hash + 二分 或者是 Manacher 算法.(后者明显比前者块)

由于这是 Manacher 的模板题,所以这里只讲第一种方法.

O(N)扫描字符串 s,建立前缀与后缀 Hash 数组.

之后枚举回文串的中心位置,二分答案即可.

ps:要分别处理奇偶长度的回文串.

 的算法,Manacher 的时间复杂度为 ,但用时 Manacher 完胜第一种.

四.代码实现:

字符串 Hash + 二分

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define lc k * 2
#define rc k * 2 + 1
#define pi acos(-1.0)
#define ll long long
#define ull unsigned long long
using namespace std;

const int M = (int)1e6;
const int mod = 99991;
const int inf = 0x3f3f3f3f;

char s[M + 5];
ull P[M + 5];
ull prefix[M + 5];
ull suffix[M + 5];

void get_sum(int len)
{
    P[0] = 1;
    suffix[len + 1] = 0;
    for(int i = 1; i <= len; ++i)
    {
        prefix[i] = prefix[i - 1] * 131 + s[i] - 'a' + 1;
        P[i] = P[i - 1] * 131;
    }
    for(int i = len; i; --i)
        suffix[i] = suffix[i + 1] * 131 + s[i] - 'a' + 1;
}

ull get_prefix(int l, int r)
{
    return prefix[r] - prefix[l - 1] * P[r - l + 1];
}

ull get_suffix(int l, int r)
{
    return suffix[l] - suffix[r + 1] * P[r - l + 1];
}

int solve()
{
    int len = strlen(s + 1);
    get_sum(len);
    int ans = 0, l, r, mid;
    for(int i = 1; i <= len; ++i)
    {
        l = 0;
        r = min(i - 1, len - i);
        while(l < r)
        {
            mid = (l + r + 1) >> 1;
            if(get_prefix(i - mid, i - 1) == get_suffix(i + 1, i + mid))
                l = mid;
            else
                r = mid - 1;
        }
        ans = max(ans, 2 * r + 1);
        l = 0;
        r = min(i, len - i);
        while(l < r)
        {
            mid = (l + r + 1) >> 1;
            if(get_prefix(i - mid + 1, i) == get_suffix(i + 1, i + mid))
                l = mid;
            else
                r = mid - 1;
        }
        ans = max(ans, r * 2);
    }
    return ans;
}

int main()
{
    int ca = 0;
    while(~scanf("%s", s + 1) && strcmp(s + 1, "END"))
        printf("Case %d: %d\n", ++ca, solve());
    return 0;
}

Manacher

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define lc k * 2
#define rc k * 2 + 1
#define pi acos(-1.0)
#define ll long long
#define ull unsigned long long
using namespace std;

const int M = (int)1e6;
const int mod = 99991;
const int inf = 0x3f3f3f3f;

char s[M + 5];
int P[2 * M + 5];
char res[2 * M + 5];

int Manacher()
{
    int len = strlen(s);
    res[0] = '$', res[1] = '#';
    for(int i = 0; i < len; ++i)
    {
        res[(i + 1) * 2] = s[i];
        res[(i + 1) * 2 + 1] = '#';
    }
    len = 2 * len + 1;
    res[len + 1] = '\0';
    int right = 0, mid = 0;
    int MaxLen = 0, MaxMid = 0;
    for(int i = 1; i <= len; ++i)
    {
        P[i] = (right > i ? min(P[2 * mid - i], right - i) : 1);
        while(res[i + P[i]] == res[i - P[i]])
            P[i]++;
        if(right < i + P[i])
        {
            mid = i;
            right = i + P[i];
        }
        if(MaxLen < P[i])
        {
            MaxMid = i;
            MaxLen = P[i];
        }
    }
    return MaxLen - 1;
}

int main()
{
    int ca = 0;
    while(~scanf("%s", s) && strcmp(s, "END"))
        printf("Case %d: %d\n", ++ca, Manacher());
    return 0;
}