diff options
Diffstat (limited to '19/day1/palindrometer.cpp')
-rw-r--r-- | 19/day1/palindrometer.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/19/day1/palindrometer.cpp b/19/day1/palindrometer.cpp new file mode 100644 index 0000000..65914fa --- /dev/null +++ b/19/day1/palindrometer.cpp @@ -0,0 +1,22 @@ +#include <bits/stdc++.h> +using namespace std; +using ll = long long; + +int P[2][100001]; + +int main() { + string S; cin >> S; + int N = S.size(); + for (int i = 0; i < 2; ++i) { + for (int j = 0, l = 0, r = 0; j < N; ++j) { + int k = (j > r ? !i : min(P[i][l + r - j + i], r - j + 1)); + while (j - k - i >= 0 && j + k < N && S[j - k - i] == S[j + k]) ++k; + P[i][j] = k--; + if (j + k > r) l = j - k - i, r = j + k; + } + } + + ll ans = 0; + for (int i = 0; i < N; ++i) ans += P[0][i] + P[1][i]; + cout << ans << '\n'; +}
\ No newline at end of file |