uva11475 - Extend to Palindrome - hash / kmp

给定一个长度至少为N的整数,任务是生成一个回文串。原代码可能存在错误,需要在输入字符串尾部添加字符确保其成为回文。解决方案是找到输入串尾部的最大回文子串,将其左侧字符逆序添加到原始串尾部。可以使用hash或KMP算法来找到最长相同部分,然后构建最小的回文串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接,进去比较慢

Description

Your task is, given an integer N, to make a palindrome (word that reads the same when you reverse it) of length at least N (1 <= N <= 100,000). 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. The input string will consist of only upper and lower case letters.

Example

Input:

aaaa
abba
amanaplanacanal
xyz 

Output:

aaaa
abba
amanaplanacanalpanama
xyzyx

题目大意:

给定一个字符串,要求在尾部添加最少字符使其成为回文串。

题目分析:

我们可以通过求尾部最大回文串, 尾部最大回文子串左边的字符即为要添加的字符, 那么将回文串左边的字符在输出原串后逆序输出即为答案。

求尾部的最大回文子串我们可以用hash和kmp求解,将原串的尾部和翻转后串的头部进行对比,求得最长相同的部分,比如abcdc和翻转后的串cdcba最长相同部分为cdc,那么对应该题答案为abcdc + ba -> abcdcba,使用hash求解时我们也可以倒着预处理,但后面求hash值时要注意。

代码:

hash

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
const int N = 1e5 + 5;
ull p[N], f[N];
ull ff[N];
char s[N];
char ss[N];
int main()
{
    while (scanf("%s", s + 1) != EOF){
        int len = strlen(s + 1);
        p[0] = 1;
        for (int i = 1; i <= len + 1; i ++){
            ss[i] = s[i];
        }
        reverse(ss + 1, ss + len + 1);
        for (int i = 1; i <= len; i ++){
            f[i] = f[i - 1] * 131  + s[i] - 'a' + 1;
            ff[i] = ff[i - 1] * 131 + ss[i] - 'a' + 1;
            p[i] = 131 * p[i - 1];
        }
        int l = 0;
        for (int i = len; i >= 0; i --){
            if (f[len] - f[len - i] * p[i] == ff[i] - ff[0] * p[i]){
                l = i;
                break;
            }
        }
        for (int i = 1; i <= len; i ++)printf("%c", s[i]);
        for (int i = l + 1; i <= len; i ++)printf("%c", ss[i]);
        printf("\n");
    }
    return 0;
}

kmp

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;
int nex[N];
char p[N];
char s[N];
int len;
void cal_next(){
    nex[1] = 0;
    for (int i = 2, j = 0; i <= len; i ++){
        while(j > 0 && p[i] != p[j + 1])j = nex[j];
        if (p[i] == p[j + 1])j ++;
        nex[i] = j;
    }
}
int kmp(){
    int i = 1, j = 1;
    while (i < len){
        if (s[i] == p[j]) i ++, j ++;
        else if (j > 1)j = nex[j - 1] + 1;
        else i ++;
        //cout << j << endl;
    }
    return j;
}
int main()
{
    while(scanf("%s", s + 1) != EOF){
        len = strlen(s + 1);
        for (int i = 1; i <= len + 1; i ++){
            p[i] = s[i];
        }
        reverse(p + 1, p + len + 1);
        cal_next();
        int j = kmp();
        //cout << j << endl;
        printf("%s", s + 1);
        for (int i = j + 1; i <= len; i ++)printf("%c", p[i]);
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值