题目描述
解题思路
题目大意: 输出最长回文子串的长度。
Manacher算法传送门
参考代码
//**********************************************
// Author: @xmzyt1996
// Date: 2015-10-26
// Name: HDU 3068.cpp
//**********************************************
#include <cstdio>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <map>
using namespace std;
#define clr(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i < b; ++i)
#define per(i, a, b) for(int i = a; i >= b; --i)
#define pt(x) cout << #x << " = " << x << endl
#define ps puts("debug~~")
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
typedef __int64 ll;
typedef pair<int, int> pii;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int MAX_N = 110010;
char s[MAX_N<<1], str[MAX_N];
int p[MAX_N<<1];
void Transform () {
int k = 0;
s[k++] = '$'; s[k++] = '#';
for (int i = 0; str[i]; ++i) {
s[k++] = str[i]; s[k++] = '#';
}
s[k] = 0;
}
int Manacher () {
Transform();
int mx = 0, id = 0, ans = 0;
clr (p, 0);
for (int i = 1; s[i]; ++i) {
p[i] = mx > i ? min(p[2*id-i], mx-i) : 1;
while (s[i+p[i]] == s[i-p[i]]) p[i]++;
if (i + p[i] > mx) {
mx = i+p[i];
id = i;
}
ans = max(ans, p[i]-1);
}
return ans;
}
int main () {
while (~scanf("%s", str))
printf("%d\n", Manacher());
return 0;
}