hdu3294(manacher)

Girls' research

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1396    Accepted Submission(s): 532


Problem 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!
回文串,输出要把原串处理成给定字符替代a以后,,最开始以为没有给定字符也是no solution,,wa了几次,,manacher算法本身就会计算回文串中心的位置,而且该点的len[i]也带表了回文串右侧最大长度,所以很好求了,把字符串处理一下,然后最长串长度为1输出no solution,否则根据manacher原理计算一下两边端点输出就行了
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=2000010;
char str[maxn];
char tmp[maxn<<1];
int len[maxn<<1];
int init(char *st)
{
    int i,len=strlen(st);
    tmp[0]='@';
    for(i=1; i<=2*len; i+=2)
    {
        tmp[i]='#';
        tmp[i+1]=st[i/2];
    }
    tmp[2*len+1]='#';
    tmp[2*len+2]='$';
    tmp[2*len+3]=0;
    return 2*len+1;
}
int manacher(char *st,int l)
{
    int mx=0,ans=0,po=0,flag=-1;
    for(int i=1; i<=l; i++)
    {
        if(mx>i)
            len[i]=min(mx-i,len[2*po-i]);
        else
            len[i]=1;
        while(st[i-len[i]]==st[i+len[i]])
            len[i]++;
        if(len[i]+i>mx)
        {
            mx=len[i]+i;
            po=i;
        }
        if(ans<len[i])
        {
            ans=len[i];
            flag=i;
        }
    }
    return flag;
}
int main()
{
    char ch[10];
    while(~scanf("%s %s",ch,str))
    {
        int l=init(str);
        int ans=manacher(tmp,l);
        int ll;
        if(ans%2==0)
        {
            ll=len[ans]-1;
            ans/=2;
            ans--;
            if(ll==1)
            {
                //cout<<"1"<<endl;
                printf("No solution!\n");
                continue;
            }
            else
            {
                ll=(ll-1)/2;
                int mm=ch[0]-'a';
                printf("%d %d\n",ans-ll,ans+ll);
                for(int i=ans-ll; i<=ans+ll; i++)
                {
                    if(str[i]-mm<='z'&&str[i]-mm>='a')
                        printf("%c",str[i]-mm);
                    else
                        printf("%c",str[i]-mm+26);
                }
                printf("\n");
            }
        }
        else
        {
            ll=len[ans]-1;
            ans--;
            ans/=2;
            ans--;
            if(ll==1)
            {
                //cout<<"3"<<endl;
                printf("No solution!\n");
                continue;
            }
            else
            {
                ll/=2;
                int mm=ch[0]-'a';
                printf("%d %d\n",ans-ll+1,ans+ll);
                for(int i=ans-ll+1; i<=ans+ll; i++)
                {
                    if(str[i]-mm<='z'&&str[i]-mm>='a')
                        printf("%c",str[i]-mm);
                    else
                        printf("%c",str[i]-mm+26);
                }
                printf("\n");
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值