数据过水

P4287 [SHOI2011] 双倍回文

LeeeeeFt @ 2024-03-30 16:47:53

RT\

```cpp #include <bits/stdc++.h> using namespace std; vector<int> manacher(string s) { string t = "#"; for (auto c : s) { t += c; t += '#'; } int n = t.size(); std::vector<int> r(n); for (int i = 0, j = 0; i < n; i++) { if (2 * j - i >= 0 && j + r[j] > i) { r[i] = min(r[2 * j - i], j + r[j] - i); } while (i - r[i] >= 0 && i + r[i] < n && t[i - r[i]] == t[i + r[i]]) { r[i] += 1; } if (i + r[i] > j + r[j]) { j = i; } } return r; } int main() { int n, ans = 0, p = 0; cin >> n; string s; cin >> s; vector<int> ma = manacher(s); for (int i = 2; i < ma.size(); i += 2) { int pos = i - ma[i] / 2; while (p + ma[p] < i) p += 2; if (p >= pos) ans = max(ans, (i - p) * 2); else { for (int j = (pos + 1) / 2 * 2; j < i; j += 2) { if (j + ma[j] > i) { ans = max(ans, (i - j) * 2); break; } } } } cout << ans; return 0; } ```

by 编程小贝壳 @ 2024-04-03 16:23:00

n^2 过百万,暴力碾标算(orz


|