hdoj 3294 Girls' research

Manacher算法详解
本文介绍了一个基于Manacher算法的问题解决方法,重点在于寻找给定字符串中最长的回文子串,并详细展示了如何通过两次转换得到最终答案的过程。此外,还提供了完整的C++代码实现。
Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1027    Accepted Submission(s): 389


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的基础题,不过需要输出最长回文。

这里需要能推导出这两个式子

int a = (ans-1)/2-(MaxL-1)/2;

int b = a+MaxL-1;

其中a为最大回文串在原字符串的起点,b为终点。ans为最大回文串在新字符串数组中的中点下标,MaxL为最大回文串长度。

代码:

#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;

const int MAXN = 200005;

int P[MAXN*2];
char S[MAXN];
char N[MAXN*2];
int MaxL,ans;

void Manacher(int lenN)
{
	int id = 0;
	int mx = 0;
	for(int i=0 ; i<lenN ; i++)
	{
		if(i<mx)
		{
			P[i] = min(P[2*id-i],mx-i);
		}
		else P[i] = 0;
		while(i-P[i]>=0 && i+P[i]<lenN &&N[i-P[i]] == N[i+P[i]])P[i]++;
		if(i+P[i]>mx)
		{
			id = i;
			mx = i+P[i];
		}
		if(P[i]-1 > MaxL)
		{
			MaxL = P[i]-1;
			ans = i;
		}
	}
}

int main()
{
	char ch;
	while(scanf("%c %s",&ch,S)!=EOF)
	{
		getchar();
		MaxL = 1;
		ans = 0;
		int mid = (int)(ch-'a');
		int len = strlen(S);
		int lenN = 0;
		for(int i=0 ; i<len ; i++)
		{
			char t = S[i] - mid;
			if(t<'a')t += 26;
			N[lenN++] = '#';
			N[lenN++] = t;
		}
		Manacher(lenN);
		if(MaxL == 1)printf("No solution!\n");
		else
		{
			int a = (ans-1)/2-(MaxL-1)/2;
			int b = a+MaxL-1;
			printf("%d %d\n",a,b);
			for(int i=ans-MaxL ; i<=ans+MaxL ; i++){
				if(i&1)printf("%c",N[i]);
			}
			printf("\n");
		}
	}

	return 0;
}


转载于:https://www.cnblogs.com/vocaloid01/p/9514147.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值