LightOJ 1258 Making Huge Palindromes(马拉车)

题目链接

题目大意:给一个串的右边添加尽量少的字符,使之成为回文串。

??如果一个串本身就是回文串的话,不需要添加任何字符它就是可以得到的最小的回文串了(废话)。如果需要添加字符的,这个串的右半部分必需全部都为回文串(可能就一个字符),如果不是,那么光在右边添加字符定是不够的。所以我们用马拉车求一遍每个位置的最大回文串的长度。然后找一个最大的回文串,其右端位于串的最右端(其实也就是让左半部分最小,这样需要添加的字符就最少)。那么怎么确定某个回文串的最右端在整个字符串的最右端呢?只要它的中心点到右端点的距离正好等于半径就行了!然后答案就是整个字符串的长度加上去掉右边回文串后左半部分的长度。

char ma[maxn], str[maxn];
int ans, len, mp[maxn];
void manacher() {
    int l = 0;
    ma[l++] = ‘$‘, ma[l++] = ‘#‘;
    for (int i = 0; i<len; ++i) {
        ma[l++] = str[i];
        ma[l++] = ‘#‘;
    }
    ma[l] = -1;
    int id = 0, mx = 0;
    for (int i = 0; i<l; ++i) {
        mp[i] = mx>i ? min(mp[id*2-i], mx-i) : 1;
        while(ma[i+mp[i]]==ma[i-mp[i]]) ++mp[i];
        if (mx<i+mp[i]) {
            mx = i+mp[i];
            id = i;
        }
    }
    for(int i=l-2;i>=2;i--)
        if(mp[i]==l-i) ans = 2*len-(mp[i]-1);
}
int main(void) {
    int t, kase = 1;
    scanf("%d", &t);
    while(t--) {
        scanf("%s", str);
        len = strlen(str);
        ans = 0;
        manacher();
        printf("Case %d: %d\n", kase++, ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/shuitiangong/p/12700367.html

时间: 2024-11-05 19:03:18

LightOJ 1258 Making Huge Palindromes(马拉车)的相关文章

LightOJ 1258 Making Huge Palindromes (回文&amp;KMP)

http://lightoj.com/volume_showproblem.php?problem=1258 A string is said to be a palindrome if it remains same when read backwards. So, 'abba', 'madam' both are palindromes, but 'adam' is not. Now you are given a non-empty string S, containing only lo

Light OJ 1258 Making Huge Palindromes 末尾添加最少字符变回文串

题目来源:Light OJ 1258 Making Huge Palindromes 题意:末尾添加最少的字符是使输入的串变成回文 输出长度 思路:直接KMP匹配出它和它反串的最大匹配 n减去它就是要添加的数量 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1000010; char a[maxn], p[maxn]; int

LightOJ 1033 - Generating Palindromes 【区间DP】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1033 题意:至少添加几个字符,能使得给定的串变为回文串. 解法:枚举起点终点,进行DP: 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string>

lightoj 1349 - Aladdin and the Optimal Invitation 贪心 中位数

1349 - Aladdin and the Optimal Invitation PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 32 MB Finally Aladdin reached home, with the great magical lamp. He was happier than ever. As he was a nice boy, he wanted to share the hap

LightOJ 1295 Lighting System Design dp

链接:http://lightoj.com/volume_showproblem.php?problem=1295 1295 - Lighting System Design PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are given the task to design a lighting system for a huge conference hall. After do

LightOJ 题目1427 - Substring Frequency (II)(AC自己主动机)

1427 - Substring Frequency (II) PDF (English) Statistics Forum Time Limit: 5 second(s) Memory Limit: 128 MB A string is a finite sequence of symbols that are chosen from an alphabet. In this problem you are given a string T and n queries each with a

LightOJ 1213 Fantasy of a Summation(规律 + 快数幂)

http://lightoj.com/volume_showproblem.php?problem=1213 Fantasy of a Summation Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1213 Description If you think codes, eat codes then sometimes you

LightOJ 1004 Monkey Banana Problem (DP 数字三角形)

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "MonkeyBanana Problem". It states that, a monkey enters into a diamond shaped twodimen

LightOJ 1349 - Aladdin and the Optimal Invitation(数学啊)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1349 Finally Aladdin reached home, with the great magical lamp. He was happier than ever. As he was a nice boy, he wanted to share the happiness with all people in the town. So, he wanted to invi