manacher java_HDU 3294Girls’ research最长回文子串(暴力和manacher 两种解法) | 学步园...

Girls' research

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 444    Accepted Submission(s): 148

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,比如是c,然后d就代表b依次类推。求出后面的串的最长回文子串。如果长度小于2输出No solution!这个题目与上一个best reward有点不同,那个题目是从中间切断判断两半是否回文,所以可以用EKMP。但是这题是让你求任意的子串。

解题思路:先给一个暴力解法,很容易理解。从任意点可以往外扩展的最长的距离。不过需要分奇偶。

暴力AC代码:

#include

#include

#include

#include

using namespace std;

char str[200005];

int main()

{

char tmp[5];

int i,p,len,j;

while(~scanf("%s %s",&tmp,str))

{

p=tmp[0]-'a';

len=strlen(str);

for(i=0;i

str[i]=(str[i]-'a'-p+26)%26+'a';

//上面是转换字符

int left,right;

int res=-1;

for(i=0;i

{

for(j=0;i-j>=0,i+j+1

{

if(str[i-j]!=str[i+j+1])

break;

if(2*j+2>res)

{

res=2*j+2;

left=i-j;

right=i+j+1;

}

}

for(j=0;i-j>=0,i+j

{

if(str[i-j]!=str[i+j])

break;

if(2*j+1>res)

{

res=2*j+1;

left=i-j;

right=i+j;

}

}

}

if(res<2)

puts("No solution!");

else

{

printf("%d %d\n",left,right);

for(i=left;i<=right;i++)

printf("%c",str[i]);

puts("");

}

}

return 0;

}

//140MS 356K

后来上网看了下,有个manacher是专门解决最长回文子串的问题。它的解法避开了奇偶的讨论,因为它在每个字符中间都插入了#然后第一个插入*。

见AC代码:

#include

#include

#include

#include

using namespace std;

char str[200005],s[400005];

int len,p[4000005];

void manacher()

{

int i;

s[0]='*',s[1]='#';

for(i=0;i

s[2*i+2]=str[i],s[2*i+3]='#';

s[len*2+2]='\0';

len=len*2+2;

int left,right;

int mx=0,id,res=-1;

//下面套用manacher模板

for(int i=2;i

{

if(mx>i)p[i]=min(p[2*id-i],mx-i);

else p[i]=1;

for(;s[i-p[i]]==s[i+p[i]];p[i]++);

if(p[i]+i>mx)mx=p[i]+i,id=i;

if(p[i]-1>res)

{

res=p[i]-1;

left=(i-p[i]+2)/2-1;

right=left+p[i]-2;

}

}

if(res<2)

puts("No solution!");

else

{

printf("%d %d\n",left,right);

for(i=left;i<=right;i++)

printf("%c",str[i]);

puts("");

}

}

int main()

{

char tmp[5];

int i,p,j;

while(~scanf("%s %s",&tmp,str))

{

p=tmp[0]-'a';

len=strlen(str);

for(i=0;i

str[i]=(str[i]-'a'-p+26)%26+'a';

//上面是转换字符

manacher();

}

return 0;

}

//250MS 1664K

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值