forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanacher.cpp
More file actions
35 lines (33 loc) · 708 Bytes
/
manacher.cpp
File metadata and controls
35 lines (33 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Count number of substrings-palindromes in given string s in O(|s|) time
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
string t;
for (char x : s) {
t += x;
t += '#';
}
int n = t.size();
long long ans = 0;
vector<int> rad(n);
for (int i = 0, l = 0, r = 0; i < n; ++i) {
if (i < r) {
rad[i] = min(r - i, rad[l + r - i]);
}
while (i + rad[i] < n && i - rad[i] >= 0 && t[i + rad[i]] == t[i - rad[i]]) {
++rad[i];
}
if (i + rad[i] > r) {
l = i - rad[i];
r = i + rad[i];
}
if (i % 2 == 0) {
ans += (rad[i] + 1) / 2 - 1;
} else {
ans += rad[i] / 2;
}
}
cout << ans << '\n';
}