Description
顺序和逆序读起来完全一样的串叫做回文串。比如acbca是回文串,而abc不是(abc的顺序为“abc”,逆序为“cba”,不相同)。
输入长度为n的串S,求S的最长双回文子串T,即可将T分为两部分X,Y,(|X|,|Y|≥1)且X和Y都是回文串。
Solution
其实可以直接上Manacher233
正反各建一个PAM,记录每次插入每个字符后的len,将两个PAM对应位置的答案加起来求最大值即可。
Code
/************************************************
* Au: Hany01
* Date: Jun 22nd, 2018
* Prob: [BZOJ2565] 最长双回文串
* Email: hany01@foxmail.com
* Institute: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 100005;
struct PalindromicTree
{
int len[maxn], fa[maxn], ch[maxn][26], mx[maxn], tot, las, n;
char s[maxn];
inline void extend(int i)
{
int p = las, c = s[i] - 97;
while (s[i - len[p] - 1] != s[i]) p = fa[p];
if (!ch[p][c]) {
int np = ++ tot, k = fa[p];
while (s[i - len[k] - 1] != s[i]) k = fa[k];
len[np] = len[p] + 2, fa[np] = ch[k][c], ch[p][c] = np;
}
mx[i] = len[las = ch[p][c]];
}
inline void build()
{
fa[0] = fa[1] = 1, len[tot = 1] = -1;
For(i, 1, n) extend(i);
}
}PT1, PT2;
int main()
{
#ifdef hany01
File("bzoj2565");
#endif
static int Ans;
scanf("%s", PT1.s + 1), PT1.n = PT2.n = strlen(PT1.s + 1);
Cpy(PT2.s, PT1.s), reverse(PT2.s + 1, PT2.s + 1 + PT1.n);
PT1.build(), PT2.build();
For(i, 1, PT1.n - 1) chkmax(Ans, PT1.mx[i] + PT2.mx[PT1.n - i]);
printf("%d\n", Ans);
return 0;
}
//春日游,杏花吹满头。
// -- 韦庄《思帝乡·春日游》