hdoj 3294 Girls' research

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!
 

Summarize:

Manacher's basic question, but it needs to output the longest palindrome.

Here we need to be able to derive these two formulas

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

int b = a+MaxL-1;

Among them, a is the starting point of the largest palindrome string in the original string, and b is the end point. ans is the subscript of the midpoint of the maximum palindrome in the new string array, and MaxL is the length of the maximum palindrome.

Code:

#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,years;

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;
			years = i;
		}
	}
}

intmain()
{
	char ch;
	while(scanf("%c %s",&ch,S)!=EOF)
	{
		getchar();
		MaxL = 1;
		years = 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 = (years-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;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326062320&siteId=291194637