hdu 3294 Girls' research Manacher回文串

Girls' research(Manacher)
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

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!
 


题意:给出一个加密的字符串,然后找字符串中的最长回文字串的起始位置和值


思路:先解密,再使用Manacher算法找出最长回文字串,其中有一个公式,新串中的位置( i , j )对应原始串的( (i-mp[i])/2 , (i+mp[i]-4)/2 ),很容易推导出来。


代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#define MAX 200010
using namespace std;

char ma[MAX<<1];
int mp[MAX<<1];

int manacher(char s[],int len)
{
    int L=0;
    ma[L++]='$';
    ma[L++]='#';
    for(int i=0;i<len;i++)
    {
        ma[L++]=s[i];
        ma[L++]='#';
    }
    ma[L]='\0';
    int mx=0,id=0;
    for(int i=0;i<L;i++)
    {
        mp[i]=mx>i?min(mp[2*id-i],mx-i):1;
        while(ma[i+mp[i]]==ma[i-mp[i]]) mp[i]++;
        if(i+mp[i]>mx)
        {
            mx=i+mp[i];
            id=i;
        }
    }
    return L;
}


int main()
{
    char s[MAX],ch[2];
    int len,nlen,i,pre,maxp,maxn;
    int bg,ed;
    while(~scanf("%s%s",ch,s))
    {
        maxp=maxn=-1;
        len=strlen(s);
        pre=ch[0]-'a';
        for(i=0;i<len;i++)
        {
            s[i]=s[i]-pre;
            if(s[i]<'a')
                s[i]+=26;
        }
        nlen=manacher(s,len);
        for(i=1;i<nlen;i++)
        {
            if(maxn<mp[i])
            {
                maxp=i;
                maxn=mp[i];
            }
        }
        if(maxn-1<2)
            printf("No solution!\n");
        else
        {
            bg=(maxp-maxn)/2;//起始位置
            ed=(maxp+maxn-4)/2;//结束位置
            printf("%d %d\n",bg,ed);
            for(i=bg;i<=ed;i++)
                printf("%c",s[i]);
            printf("\n");
        }


    }
    return 0;
}




1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看rEADME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通;、 3本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看ReAdmE.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值