string rev(string s){ reverse(ALL(s)); return s; } string prefix_palindrome(const string& s){ VI pref(s.size()*2 + 10); string a = rev(s); a = s + "#" + a; ll c = 0; for (int i = 1; i < (int)a.size(); i++){ while (c != 0 && a[c] != a[i]) c = pref[c - 1]; if (a[c] == a[i]) c++; pref[i] = c; } return s.substr(0, c); }
verified
- D1. Prefix-Suffix Palindrome
- D2. Prefix-Suffix Palindrome (Hard version)