HDU3294 Girls' research 马拉车模板题

本文介绍了一种算法,用于解决给定一个字符和字符串,通过特定转换后找到最长回文子串的问题。该算法首先根据给定字符调整字符串,然后使用Manacher算法找出最长回文串的位置和内容。

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

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!

 

题意:给出一个字符c和一串字符串s,要将s进行转换,转换的方法是将字符c转换成a,其余的也要跟着相应变化。比如字符c为b时,'b'要变成'a','c'要变成'b',。。。。。最后输入最长回文(长度大于等于2)起始位置和终止位置以及回文串。再就是求起始和终止位置有点不太明白。。借鉴了网上的方法。。就暂且先记住吧

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <map>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=2*1e5+5;
int p[maxn*2];
char s[maxn*2];
int n;
int idx;
int manachar()
{
    memset(p,0,sizeof(p));
    int id=0;
    int maxlen=0;
    int len=strlen(s);
    for(int i=len;i>=0;i--)
    {
        s[i+i+2]=s[i];
        s[i+i+1]='#';
    }
    s[0]='*';
    for(int i=2;i<len+len+1;i++)
    {
        if(p[id]+id>i)
            p[i]=min(p[2*id-i],p[id]+id-i);
        else
            p[i]=1;
        while(s[i-p[i]]==s[i+p[i]])
            p[i]++;
        if(id+p[id]<i+p[i])
            id=i;
        if(maxlen<p[i])
        {
            maxlen=p[i];
            idx=i;//后面求起始和终止位置用到
        }
    }
    return maxlen-1;
}
int main()
{
    char c[10];
    while(scanf("%s%s",c,s)!=EOF)
    {
        int d=c[0]-'a';
        int len=strlen(s);
        for(int i=0;i<len;i++)
        {
            s[i]-=d;
            if(s[i]<'a')
                s[i]+=26;
        }
        int ans=manachar();
        if(ans<2)
        {
            printf("No solution!\n");
            continue;
        }
        int L=idx-ans+1;
        int R=idx+ans-1;
        printf("%d %d\n",(L-2)/2,(R-2)/2);//起始和终止位置
        for(int i=L;i<=R;i++)
        {
            if(s[i]!='#')
                printf("%c",s[i]);
        }
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值