Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cstring>
- #include <map>
- using namespace std;
- typedef long long ll;
- string manachers_algorithm(string s) {
- if(s.empty()) {
- return "";
- }
- string t = "^";
- for(char c: s) {
- t += "#";
- t += c;
- }
- t += "#$";
- int n = (int) t.length();
- vector<int> P(n, 0);
- int C = 0, R = 0;
- int max_length = 0, center_idx = 0;
- for(int i = 1; i < n - 1; i++) {
- int i_mirror = 2 * C - i;
- if(R > i) {
- P[i] = min(R - i, P[i_mirror]);
- }
- while(t[i + 1 + P[i]] == t[i - 1 - P[i]]) {
- P[i]++;
- }
- if(i + P[i] > R) {
- C = i;
- R = i + P[i];
- }
- if(P[i] > max_length) {
- max_length = P[i];
- center_idx = i;
- }
- }
- int S = (center_idx - max_length) / 2;
- return s.substr(S, max_length);
- }
- int main() {
- ios_base::sync_with_stdio(false);
- string s;
- cin >> s;
- cout << manachers_algorithm(s) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment