ICPC Central Europe Regional Contest 2019 A. ABB(马拉车,哈希)

Fernando受聘于滑铁卢大学,任务是在已建部分的基础上,通过增加并适当涂色新小屋,使街道从小湖到森林两端颜色对称,形成回文序列。此问题转化为在字符串后添加最少字符使其成为回文串。

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

Fernando was hired by the University of Waterloo to finish a development project the university started some time ago. Outside the campus, the university wanted to build its representative bungalow street for important foreign visitors and collaborators.

Currently, the street is built only partially, it begins at the lake shore and continues into the forests, where it currently ends. Fernando’s task is to complete the street at its forest end by building more bungalows there. All existing bungalows stand on one side of the street and the new ones should be built on the same side. The bungalows are of various types and painted invarious colors.

The whole disposition of the street looks a bit chaotic to Fernando. He is afraid that it will look even more chaotic when he adds new bungalows of his own design. To counter balance the chaosof all bungalow shapes, he wants to add some order to the arrangement by choosing suitablecolors for the new bungalows. When the project is finished, the whole sequence of bungalowcolors will be symmetric, that is, the sequence of colors is the same when observed from eitherend of the street.

Among other questions, Fernando wonders what is the minimum number of new bungalows heneeds to build and paint appropriately to complete the project while respecting his self-imposed bungalow color constraint.

Input Specification

The first line contains one integer N (1 \leq N \leq 4·10^{5})N(1≤N≤4⋅10
5
), the number of existing bungalows in the street. The next line describes the sequence of colors of the existing bungalows, from the beginning of the street at the lake. The line contains one string composed of N lowercase letters(“a” through “z”), where different letters represent different colors.

Output Specification

Output the minimum number of bungalows which must be added to the forest end of the street and painted appropriately to satisfy Fernando’s color symmetry demand.

样例输入1复制
3
abb
样例输出1复制
1
样例输入2复制
12
recakjenecep
样例输出2复制
11

题意:
在字符串后面添加一些字符使得其称为回文串。

思路:
只需要找到最大的回文串后缀即可。
比赛的时候用的马拉车,但是哈希也完全ok。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1e6 + 7;
char s1[maxn],s2[maxn];
int f[maxn];

int manacher(int len)
{
    int ans = 0,id = 1,mx = 0;
    for(int i = 1;i < len;i++)
    {
        if(id + mx > i)f[i] = min(f[id * 2 - i],id + mx - i);
        while(i - f[i] - 1 >= 1 && i + f[i] + 1 <= len && s2[i - f[i] - 1] == s2[i + f[i] + 1])
            f[i]++;
        if(id + mx < i + f[i])
        {
            id = i;mx = f[i];
        }
        ans = max(ans,f[i]);
    }
    return ans;
}

int main()
{
    int n;scanf("%d",&n);
    scanf("%s",s1 + 1);
    int len = (int)strlen(s1 + 1);
    int len2 = 0;
    for(int i = 1;i <= len;i++)
    {
        s2[++len2] = '#';
        s2[++len2] = s1[i];
    }
    s2[++len2] = '#';
    manacher(len2);
    int ans = 0;
    for(int i = 1;i <= len2;i++) {
        if(f[i] >= len2 - i) {
            ans = max(ans,f[i]);
        }
    }

    printf("%d\n",n - ans);
    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef unsigned long long ull;

const int maxn = 4e5 + 7;
const int mod = 1e8 + 7;
char s[maxn];

ull h1[maxn],h2[maxn],jie[maxn];

bool check1(int x,int y) { //以x为中心
    ull H1 = h1[x - 1] - h1[x - 1 - y] * jie[y];
    ull H2 = h2[x + 1] - h2[x + y + 1] * jie[y];
    return H1 == H2;
}

bool check2(int x,int y) { //x为右半部分左端点
    ull H1 = h1[x - 1] - h1[x - 1 - y] * jie[y];
    ull H2 = h2[x] - h2[x + y] * jie[y];
    return H1 == H2;
}

int main() {
    int n;scanf("%d",&n);
    scanf("%s",s + 1);
    jie[0] = 1;
    for(int i = 1;i <= n;i++) {
        jie[i] = jie[i - 1] * mod;
        h1[i] = h1[i - 1] * mod + s[i];
    }
    for(int i = n;i >= 1;i--) {
        h2[i] = h2[i + 1] * mod + s[i];
    }
    
    int ans = 0;
    for(int i = n;i >= n / 2;i--) {
        if(i - 1 >= n - i && check1(i,n - i)) {
            ans = max(2 * (n - i) + 1,ans);
        }
        if(i - 1 >= n - i + 1 && check2(i,n - i + 1)) {
            ans = max(2 * (n - i + 1),ans);
        }
    }
    printf("%d\n",n - ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值