hdu3294_Girls' research_Manacher(马拉车算法)

Girls' research

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


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’进行求差,然后再给你一串字符串,将这字符串中的每个字符进行减去前面求得的差值,得到一个新的字符串。

然后求取这个字符串中的最长回文串,输出其起始下标和最长的回文串是哪一个。

解:

其实求差完全没有影响嘛,╮(╯_╰)╭,主要是理解了马拉车模板后,就能很好的利用模板找到起始位置了,说白了,这道题还是基于理解马拉车的基础上的水题。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define MANX 200005
using namespace std;
int l=-1,r=-1;
char str0[MANX],str[MANX<<1];
int p[MANX<<1],len;
void start()
{
    str[0]='$',str[1]='#';
    len=strlen(str0);
    for(int i=0;i<len;i++)
    {
        str[i*2+2]=str0[i];
        str[i*2+3]='#';
    }
    str[len*2+2]='\0';
}
int solve()
{
    memset(p,0,sizeof(p));
    int mx=0,id,ans=1;
    for(int i=1;i<len*2+2;++i)
    {
        if(mx>i)p[i]=min(p[2*id-i],mx-i);
        else p[i]=1;
        for(;str[i-p[i]]==str[i+p[i]];p[i]++);
        if(p[i]+i>mx)mx=p[i]+i,id=i;
        if(p[i]>ans){ans=p[i];l=i-p[i]+2;r=i+p[i]-2;}//找到最长回文串的起始位置,及长度
    }
    return --ans;
}
/*void print()//查看p[i]数组的函数,可忽略
{
    for(int i=0;i<len*2+2;i++)
    printf("%d ",p[i]);
    cout<<endl;
}*/
int main()
{
    char c[5];
    while(~scanf("%s",c))
    {
        int chazhi=c[0]-'a';
        scanf("%s",str0);
        len=strlen(str0);
        for(int i=0;i<len;i++)
        {
            str0[i]=(str0[i]-chazhi-'a'+26)%26+'a';//求得变换后的字符串
        }
        start();
        int ans=solve();
        /*print();
        cout<<"L:"<<l<<"R:"<<r<<endl;*/
        if(ans<2)printf("No solution!");//长度小于2则没有符合题意的
        else
        {
            printf("%d %d\n",(l-2)/2,(r-2)/2);//倒着算一下str原位置
            for(int i=l;i<=r;i+=2)printf("%c",str[i]);
            cout<<endl;
        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值