HDU 3294 Girls' research (Manacher算法 + 記錄區間)
Girls' research
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 566 Accepted Submission(s): 212
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!
Author
wangjing1111
Source
2010 “HDU-Sailormoon” Programming
Contest
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=3294
題目大意:輸入一個字符ch和一個字符串,問如果把ch當作'a'的話,字符串的每個字符也要做相應變化,如b aa,若b為'a',則b前面的a就為'a'前面的'z',這裡是循環表示,輸出字符串的最長回文子串,如果最長回文子串串長為1,輸出No solution!
題目分析:字符串根據要求變換一下,跑一次Manacher,每次更新maxl時,分別記錄左右端點在原串中的位置
#include
#include
#include
using namespace std;
int const MAX = 200005;
char s[MAX << 1], save[MAX << 1];
int p[MAX << 1], l, r;
int Manacher()
{
int len = strlen(s), maxp = 0, maxl = 0;
for(int i = len; i >= 0; i--)
{
s[i * 2 + 2] = s[i];
s[i * 2 + 1] = '#';
}
s[0] = '*';
for(int i = 2; i < 2 * len + 1; i++)
{
if(p[maxp] + maxp > i)
p[i] = min(p[2 * maxp - i], p[maxp] + maxp - i);
else
p[i] = 1;
while(s[i - p[i]] == s[i + p[i]])
p[i]++;
if(p[maxp] + maxp < i + p[i])
maxp = i;
if(maxl < p[i])
{
l = (i - p[i]) / 2;
r = (i + p[i]) / 2 - 2;
maxl = p[i];
}
}
return maxl - 1;
}
int main()
{
int ans;
char ch[2];
while(scanf("%s %s",ch, s) != EOF)
{
int len = strlen(s);
l = r = 0;
for(int i = 0; i < len; i++)
{
int tmp = s[i] - ch[0];
if(tmp < 0)
tmp = 26 + tmp;
s[i] = 'a' + tmp;
}
strcpy(save, s);
int ans = Manacher();
if(ans == 1)
printf("No solution!\n");
else
{
printf("%d %d\n", l, r);
for(int i = l; i <= r; i++)
printf("%c", save[i]);
printf("\n");
}
}
}