首页 > 其他 > 详细

Girls' research(manacher)

时间:2015-11-25 23:27:40      阅读:263      评论:0      收藏:0      [点我收藏+]

 

 

Girls‘ research

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

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保存位置;然后再转义字符;

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
const int MAXN=200010;
char s[MAXN],a[MAXN<<1];
int ans,ps;
int p[MAXN<<1];
void manacher(){
	int m=1,l=1,r=1;
	 ans=0;ps=0;
	int len=strlen(a);
	p[0]=p[1]=1;
	for(int i=2;i<len;i++){
		if(r>i)p[i]=min(p[m-(i-m)],r-i);
		else p[i]=1;
		while(a[i-p[i]]==a[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,ps=i;
	}
}
int main(){
	char ch[2];
	while(~scanf("%s%s",ch,s)){
		int len=strlen(s);
		a[0]=‘#‘;
		for(int i=0;i<len;i++){
			a[i*2+1]=s[i];
			a[i*2+2]=‘#‘;
		}
		a[len*2+1]=‘#‘;a[len*2+2]=‘\0‘;
		manacher();
		//printf("%d\n",ans);
		int l=(ps-ans+1)/2,r=l+ans-1;
		if(ans==1){
			puts("No solution!");continue;
		}
		printf("%d %d\n",l,r);
		int t=ch[0]-‘a‘;
		for(int i=l;i<=r;i++){
			if(s[i]-‘a‘>=t)printf("%c",s[i]-t);
			else printf("%c",s[i]-t+26);
		}puts("");
	}
	return 0;
}

  

 

Girls' research(manacher)

原文:http://www.cnblogs.com/handsomecui/p/4996084.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!