您的位置:首页 > 其它

hdu3294(manacher)

2016-04-15 17:22 274 查看


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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: