Skip to content

Commit

Permalink
Finish LuoguP3805: manacher
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaiser-Yang committed Jul 2, 2024
1 parent 4286b90 commit f799dad
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Luogu/LuoguP3805.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// problem statement: https://www.luogu.com.cn/problem/P3805

#include <bits/stdc++.h>

constexpr size_t MAXN = 1.1e7;

using namespace std;

string str;

size_t manacher(const string &src) {
string s;
s.push_back('#');
for (auto &&ch : src) {
s.push_back(ch);
s.push_back('#');
}
s.push_back('#');
size_t ans = 0;
vector<size_t> p(s.size());
p[0] = 1;
size_t id = 0, max_right = 0;
for (size_t i = 1; i < s.length(); i++) {
if (i < max_right) {
p[i] = min(p[2 * id - i], max_right - i + 1);
} else {
p[i] = 1;
}
while (i + p[i] < s.length() && i >= p[i] && s[i + p[i]] == s[i - p[i]]) {
p[i]++;
}
if (i + p[i] - 1 > max_right) {
max_right = i + p[i] - 1;
id = i;
}
ans = max(ans, p[i] - 1);
}
return ans;
}

int main() {
ios::sync_with_stdio(false);
cin >> str;
cout << manacher(str) << '\n';
return 0;
}

0 comments on commit f799dad

Please sign in to comment.