HDU 3294 Girls‘s research(马拉车)

One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps:
First step: girls will write a long string (only contains lower case) on the paper. For example, “abcde”, but ‘a’ inside is not the real ‘a’, that means if we define the ‘b’ is the real ‘a’, then we can infer that ‘c’ is the real ‘b’, ‘d’ is the real ‘c’ ……, ‘a’ is the real ‘z’. According to this, string “abcde” changes to “bcdef”.
Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.
Input
Input contains multiple cases.
Each case contains two parts, a character and a string, they are separated by one space, the character representing the real ‘a’ is and the length of the string will not exceed 200000.All input must be lowercase.
If the length of string is len, it is marked from 0 to len-1.
Output
Please execute the operation following the two steps.
If you find one, output the start position and end position of palindromic string in a line, next line output the real palindromic string, or output “No solution!”.
If there are several answers available, please choose the string which first appears.
Sample Input
b babd
a abcd
Sample Output
0 2
aza
No solution!

题意:
题目上说的很明确了 寻找最大回文串 就是一道马拉车的裸板子题 加上了一个字符的转换 按照所给的字符与字符a的差值来确定新的字符 这种字符串之间的转换可以很容易的完成 
建议诸如马拉车此类的优化算法 尽量使用scanf去控制输入 减少时间

如果对马拉车不太了解 请点击这里 一篇很精彩的博客


2020.5.22补充:
https://leetcode-cn.com/problems/longest-palindromic-substring/solution/zui-chang-hui-wen-zi-chuan-by-leetcode-solution/

视屏中对马拉车的解释从12:43开始,非常详细。


上面的这篇博客其中对字符串刚开始时的预处理采用了string 其实string进行运算符重载 例如(+),是非常耗费时间的,因为string这种顺序容器会使得插入点之后的迭代器会失效 也就是会重新分配一部分内存 这是很浪费时间的 我们可以用一个数组实现这种情况 也很简单 就是用一个提前分配内存的数组 逆向遍历字符串 填到数组中即可 省去了每次分配内存的开销

ac代码

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<cstdio>
#include<cstring>
#include<unordered_map>
#include<cstring>
using namespace std;

char ch;
char str[400000+100];
int  p[400000+100];
char temp[400000+100];
int mx,id,flag;
int radius;
int center;

int init(char a[])
{
    int ans=1;
    int len=strlen(a);
    for(int i=len;i>=0;i--)
    {
        a[2*i+1]='#';
        a[2*i+2]=a[i];
        ans+=2;
    }
    a[0]='$';
    a[ans]='\0';
    return ans;
}

void Manachar(char *str)
{
    id=0;mx=0;
    flag=init(str);//处理后的长度 其实可以算
    for(int i=1;i<flag;i++)
    {
        p[i]=mx>i?min(p[2*id-i],mx-i):1;
        while(str[i+p[i]]==str[i-p[i]]) p[i]++;
        if(mx<i+p[i])
        {
            mx=i+p[i];
            id=i;
        }
        if(radius<p[i])
        {
            radius=p[i];
            center=i;
        }
    }
}
int main()
{
    while(~scanf("%c %s",&ch,str))
    {
        getchar();
        memset(p,0,sizeof(p));
        radius=0,center=0;
        mx=0,id=0;
        strcpy(temp,str);
        Manachar(str);
        int x=(center-radius)/2+radius-1;
        int y=(center-radius)/2;
        if(x-y<2)
        {
            printf("No solution!\n");
        }else{
            printf("%d %d\n",y,x-1);
            int ans=ch-'a';
            for(int i=y;i<x;i++)
            {
                printf("%c",temp[i]-ans-'a'>=0?temp[i]-ans:temp[i]+26-ans);
            }
            putchar('\n');
        }
    }
}
posted @ 2022-07-02 13:18  李兆龙的博客  阅读(12)  评论(0编辑  收藏  举报