您的位置:首页 > 其它

hdoj Girls' research (字符串&manacher)

2015-12-02 19:25 344 查看

Girls' research

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

Total Submission(s): 1164    Accepted Submission(s): 450


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

b babd
a abcd

[align=left]Sample Output[/align]

0 2
aza
No solution! 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 200010
using namespace std;
char s[N<<1];
char a[N<<1];
int p[N<<1];
int ans,pp;
void manacher()
{
int m=1,r=1;
ans=0;pp=0;
int l=strlen(s),i;
memset(p,0,sizeof(p));
p[0]=p[1]=1;
for(i=2;i<l;i++)
{
if(r>i)
p[i]=min(p[m-(i-m)],r-i);
else
p[i]=1;
while(s[i-p[i]]==s[i+p[i]])
p[i]++;
if(p[i]+i>r)
{
r=p[i]+i;
m=i;
}
if(p[i]-1>ans)
{
ans=p[i]-1;
pp=i;
}
}
}
int main()
{
int ll,i,j;
char c[2];
while(scanf("%s%s",c,a)!=EOF)
{
int ll=strlen(a);
s[0]='@';
for(i=0;i<ll;i++)
{
s[i*2+1]='#';
s[i*2+2]=a[i];
}
s[ll*2+1]='#';
s[ll*2+2]='\0';
manacher();
int l=(pp-ans)/2;
int r=l+ans-1;
if(ans==1)
printf("No solution!\n");
else
{
printf("%d %d\n",l,r);
int t=c[0]-'a';
for(i=l;i<=r;i++)
{
if(a[i]-'a'>=t)
printf("%c",a[i]-t);
else
printf("%c",a[i]-t+26);
}
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: