1
0
Fork 0

Add string/manacher.cc

Signed-off-by: subcrip <contact@subc.rip>
This commit is contained in:
subcrip 2024-11-24 17:18:56 +00:00
parent 562bf6c4b8
commit c3ea7e194a
1 changed files with 28 additions and 0 deletions

28
string/manacher.cc Normal file
View File

@ -0,0 +1,28 @@
template <typename T>
vector<int> manacher(const vector<T>& s, const T& never) {
vector<T> t = { never };
for (auto&& c : s) {
t.emplace_back(c);
t.emplace_back(never);
}
int n = t.size();
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;
}
}
for (int i = 0; i < n; i += 2) {
r[i] = (r[i] - 1) / 2;
}
for (int i = 1; i < n; i += 2) {
r[i] /= 2;
}
return r;
}