From 34492d4ba8623e9968667e6c7a77a81bb169ea93 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 27 Jun 2022 19:31:39 +0800 Subject: [PATCH] =?UTF-8?q?P4555=20[=E5=9B=BD=E5=AE=B6=E9=9B=86=E8=AE=AD?= =?UTF-8?q?=E9=98=9F]=E6=9C=80=E9=95=BF=E5=8F=8C=E5=9B=9E=E6=96=87?= =?UTF-8?q?=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/77999110 --- Luogu/P4555/P4555.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Luogu/P4555/P4555.cpp diff --git a/Luogu/P4555/P4555.cpp b/Luogu/P4555/P4555.cpp new file mode 100644 index 00000000..61ec993b --- /dev/null +++ b/Luogu/P4555/P4555.cpp @@ -0,0 +1,57 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int mid, r, p[N << 1], a[N << 1], b[N << 1], ans; +std::string s1, s2; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> s1; + + s2.push_back('^'); + + for (const char &c : s1) { + s2.push_back('#'); + s2.push_back(c); + } + + s2 += "#$"; + + for (int i = 1; i < s2.size(); i++) { + p[i] = i < r ? std::min(p[mid * 2 - i], r - i) : 1; + while (s2[i - p[i]] == s2[i + p[i]]) p[i]++; + if (i + p[i] > r) { + r = i + p[i]; + mid = i; + } + + a[i + p[i] - 1] = std::max(a[i + p[i] - 1], p[i] - 1); + b[i - p[i] + 1] = std::max(b[i - p[i] + 1], p[i] - 1); + } + + for (int i = 3; i <= s2.size() - 4; i += 2) { + b[i] = std::max(b[i], b[i - 2] - 2); + } + + for (int i = s2.size() - 4; i >= 3; i -= 2) { + a[i] = std::max(a[i], a[i + 2] - 2); + } + + for (int i = 3; i <= s2.size() - 4; i += 2) { + if (a[i] && b[i]) { + ans = std::max(ans, a[i] + b[i]); + } + } + + cout << ans << endl; + + return 0; +}