题意
You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions:
- The length of t does not exceed the length of s.
- t is a palindrome.
- There exists two strings a and b (possibly empty), such that t=a+b ( “+” represents concatenation), and a is prefix of s while b is suffix of s.
记找s的一个不重叠的前缀a和后缀b,使得a+b是回文串且尽量长
样例
input:
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba
output:
a
abcdfdcba
xyzyx
c
abba
首先前后一一匹配,比方说abccbba,找出前缀ab后缀ba,剩下的ccb找一个最长回文前缀(cc),后缀(b)选最长的
最长回文前缀问题可以用字符串哈希解决,求一遍正向哈希,再求一遍倒着的哈希,如果是回文hash值相同,这里用的是BDKRHash,它可以O(1)求子串hash值,另外hash是非完美算法,可以在O(n)检验一下是不是回文串
#include <iostream>
#include <cstdio>
using namespace std;
typedef unsigned long long LL;
#define base 33
LL p[1000100];
LL h1[1000100],h2[1000100];
inline LL gethash1(int l,int r) {
LL tp = l?h1[l-1]:0;
return h1[r] - tp*p[r-l+1];
}
inline LL gethash2(int l,int r) {
LL tp = l?h2[l-1]:0;
return h2[r