Manacher

#include <bits/stdc++.h>
#define il inline

using namespace std;

bool Beg;
namespace Zctf1088 {
namespace IO {
	const int bufsz = 1 << 20;
	char ibuf[bufsz], *p1 = ibuf, *p2 = ibuf;
	#define getchar() (p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, bufsz, stdin), p1 == p2) ? EOF : *p1++)
	il int read() {
		int x = 0; char ch = getchar(); bool t = 0;
		while (ch < '0' || ch > '9') {t ^= ch == '-'; ch = getchar();}
		while (ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();}
		return t ? -x : x;
	}
	char obuf[bufsz], *p3 = obuf, stk[50];
	#define flush() (fwrite(obuf, 1, p3 - obuf, stdout), p3 = obuf)
	#define putchar(ch) (p3 == obuf + bufsz && flush(), *p3++ = (ch))
	il void write(int x, bool t = 0) {
		int top = 0;
		x < 0 ? putchar('-'), x = -x : 0;
		do {stk[++top] = x % 10 | 48; x /= 10;} while(x);
		while (top) putchar(stk[top--]);
		t ? putchar(' ') : putchar('\n');
	}
	struct FL {
//		~FL() {flush();}
	} fl;
}
using IO::read; using IO::write;
const int N = 3e7 + 10;
char s[N], t[N];
int n, m;
int d[N];
il void manacher(int n) {
	int l = 1, r = 0;
	for (int i = 1; i <= n; i++) {
		int k = (i > r ? 1 : min(d[l + r - i], r - i + 1));
		while (i - k >= 1 && i + k <= n && t[i - k] == t[i + k]) k++;
		d[i] = k;
		if (i + k - 1 > r) 
			l = i - k + 1, r = i + k - 1;
	}
}
signed main() {
	ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
	cin >> (s + 1);
	n = strlen(s + 1);
	for (int i = 1; i <= n; i++) {
		t[++m] = '#';
		t[++m] = s[i];
	}
	t[++m] = '#';
	manacher(m);
	int ans = 0;
	for (int i = 1; i <= m; i++) 
		ans = max(ans, d[i] - 1);
	cout << ans << "\n";
	return 0;
}}
bool End;
il void Usd() {cerr << "\nUse: " << (&Beg - &End) / 1024.0 / 1024.0 << "MB " << (double)clock() * 1000.0 / CLOCKS_PER_SEC << "ms\n";}
signed main() {
	Zctf1088::main();
	Usd();
	return 0;
}
posted @ 2026-09-21 22:25  Zctf1088  阅读(18)  评论(0)    收藏  举报