aboutsummaryrefslogtreecommitdiff
path: root/2019/day1/palindrometer.cpp
blob: 65914fada309710b6be7e812af2f27d0e882f2a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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';
}