Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) of length at least N. Any palindrome will do. Easy, isn’t it? That’s what you thought before you passed it on to your inexperienced team-mate. When the contest is almost over, you find out that that problem still isn’t solved. The problem with the code is that the strings generated are often not palindromic. There’s not enough time to start again from scratch or to debug his messy code. Seeing that the situation is desperate, you decide to simply write some additional code that takes the output and adds just enough extra characters to it to make it a palindrome and hope for the best. Your solution should take as its input a string and produce the smallest palindrome that can be formed by adding zero or more characters at its end.
Input
Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of upper case and lower case English letters (‘A’-‘Z’ and ‘a’-‘z’). The length of the string will be less than or equal to 100,000.
Output
For each line of input, output will consist of exactly one line. It should contain the palindrome formed by adding the fewest number of extra letters to the end of the corresponding input string.
Sample Input
aaaa
abba
amanaplanacanal
xyz
Sample Output
aaaa
abba
amanaplanacanalpanama
xyzyx
题意: 给出一个字符串,可以在其后面追加任意个字符使其成为回文串。输出添加最少字符生成的回文串。
分析: 模拟样例可以发现,如果把给的字符串倒序拼接上去一定是回文串,但是可能存在更短的回文串,于是可以找出最长回文后缀,把前面的部分倒序拼接上去就是最短的回文串。找到最长回文后缀可以通过后缀数组或者kmp来解决,思路都是把字符串先倒序,其后面连接上原字符串,kmp做法直接求出最长公共前后缀,它就是最长回文后缀,后缀数组做法枚举原串的后缀起始位置i求出lcp(1, i)等于后缀i长度的第一个位置,这就是最长的回文后缀。
具体代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1e6+10;
int n, m, len;
char s[maxn];
int sa[maxn], height[maxn], x[maxn], y[maxn], rk[maxn], tong[maxn], f[maxn][20];
char t[maxn];
void get_sa()
{
for(int i = 0; i <= m; i++) tong[i] = 0;
for(int i = 0; i <= 2*n; i++) y[i] = x[i] = 0;
for(int i = 1; i <= n; i++) tong[x[i] = s[i]] ++;
for(int i = 2; i <= m; i++) tong[i] += tong[i-1];
for(int i = n; i; i--) sa[tong[x[i]]--] = i;
for(int k = 1; k <= n; k <<= 1)
{
int num = 0;
for(int i = n-k+1; i <= n; i++) y[++num] = i;
for(int i = 1; i <= n; i++)
{
if(sa[i] <= k) continue;
y[++num] = sa[i] - k;
}
for(int i = 0; i <= m; i++) tong[i] = 0;
for(int i = 1; i <= n; i++) tong[x[i]]++;
for(int i = 2; i <= m; i++) tong[i] += tong[i-1];
for(int i = n; i; i--) sa[tong[x[y[i]]]--] = y[i], y[i] = 0;
for(int i = 0; i <= 2*num; i++)
{
int temp = x[i];
x[i] = y[i];
y[i] = temp;
}
x[sa[1]] = 1, num = 1;
for(int i = 2; i <= n; i++)
x[sa[i]] = (y[sa[i]] == y[sa[i-1]] && y[sa[i] + k] == y[sa[i-1] + k]) == 1 ? num : ++ num;
if(n == num) return;
m = num;
}
}
void get_height()
{
for(int i = 1; i <= n; i++) rk[sa[i]] = i;
for(int i = 1, k = 0; i <= n; i++)
{
if(rk[i] == 1) continue;
if(k) k--;
int j = sa[rk[i]-1];
while(i + k <= n && j + k <= n && s[i+k] == s[j+k]) k++;
height[rk[i]] = k;
}
}
void st()
{
for(int i = 1; i <= n; i++)
f[i][0] = height[i];
for(int j = 1; j < 20; j++)
for(int i = 1; i+(1<<j)-1 <= n; i++)
f[i][j] = min(f[i][j-1], f[i+(1<<(j-1))][j-1]);
}
int query(int l, int r)
{
if(l > r)
swap(l, r);
l++;
int t = log(r-l+1)/log(2);
return min(f[l][t], f[r-(1<<t)+1][t]);
}
int solve()
{
get_sa();
get_height();
st();
for(int i = 1; i <= len; i++)
{
if(query(rk[i], rk[len+2]) == len-i+1)
return i;
}
}
signed main()
{
while(~scanf("%s", t+1))
{
len = strlen(t+1);
for(int i = 1; i <= len; i++)
s[i] = t[i];
s[len+1] = '0';//分隔符
for(int i = len+2; i <= 2*len+1; i++)
s[i] = t[len-(i-len-2)];
n = 2*len+1;
m = 200;
int res = solve()-1;
printf("%s", t+1);
for(int i = res; i >= 1; i--)
putchar(t[i]);
puts("");
}
return 0;
}