[最长回文后缀][后缀数组]Extend to Palindrome UVA11475

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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值