HDU3294 Girls' research

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 
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!

正解:manacher

解题报告:

  manacher裸题。

  按题意所说的做就可以了,注意分奇数和偶数的情况讨论,当然也可以不讨论,弄出一个公共的式子即可。

  开始没策清楚下标关系,WA了一发...

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 400011;
int len,n,cha,p[MAXN],ans;
char ch[MAXN],s[MAXN],H;
inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}

inline void manacher(){
	memset(p,0,sizeof(p)); int maxR=0,id=0;
	for(int i=1;i<=n;i++) {
		if(i<maxR) p[i]=min(p[2*id-i],maxR-i); else p[i]=1;
		for(;i+p[i]<=n && s[i-p[i]]==s[i+p[i]];p[i]++) ;
		if(i+p[i]>maxR) { maxR=i+p[i]; id=i; }
	}
}

inline void work(){
	while(scanf("%s",ch)!=EOF) {
		H=ch[0]; scanf("%s",ch); len=strlen(ch); n=1;
		cha=H-'a'; s[0]='%'; s[1]='#';	   
		for(int i=0;i<len;i++) {
			ch[i]-=cha; if(ch[i]<'a') ch[i]+=26;
			s[++n]=ch[i];
			s[++n]='#';
		}
		manacher(); ans=1; int pos=1;
		for(int i=1;i<=n;i++) if(p[i]>ans) ans=p[i],pos=i;
		ans--; if(ans==1) { printf("No solution!
"); continue; }
		int l,r,mid;
		if(pos%2==0) { mid=pos/2-1; l=mid-ans/2; r=mid+ans/2; }
		else { mid=pos/2-1; l=mid-ans/2+1; r=mid+ans/2; }
		printf("%d %d
",l,r);
		for(int i=l;i<=r;i++) printf("%c",ch[i]);
		printf("
");
	}
}

int main()
{
    work();
    return 0;
}

  

原文地址:https://www.cnblogs.com/ljh2000-jump/p/6266316.html