【题解】HDU-3294 Girls' research

Girls’ research(HDU-3294)

题面

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 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.

输出

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.

样例输入

1
2
b babd
a abcd

样例输出

1
2
3
0 2
aza
No solution!

提示

思路

Manacher,字符置换不影响回文性质,先求出最长回文子串长度和起点,输出的时候替换一下即可。

代码

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
char s[mxn], t[mxn];
int p[mxn], l;

int manacher_init(char *s, char *t, int n)
{
int j = 2;
t[0] = -2, t[1] = -1;

for (int i = 0; i < n; i++)
{
t[j++] = s[i];
t[j++] = -1;
}
t[j] = -3;
return j;
}

int manacher(char *t, int *p, int n)
{
int id = 0, mx = 0, ans = 0;
for (int i = 1; i <= n; i++)
{
p[i] = i<mx ? min(p[2*id-i], mx-i) : 1;

while (t[i+p[i]] == t[i-p[i]]) p[i]++;

if (mx < i+p[i])
mx = i+p[i], id = i;

if(ans < p[i]){
ans = p[i];
l = (i-p[i])/2;
}
}
return ans-1;
}

int main()
{
char c;
while(~scanf(" %c %s", &c, s))
{
int n = manacher_init(s, t, strlen(s));
int ans = manacher(t, p, n);
if(ans>2){
printf("%d %d\n", l, l+ans-1);
for(int i=0; i<ans; i++){
printf("%c", (s[i+l]-c+26)%26+'a');
}
printf("\n");
}else{
printf("No solution!\n");
}
}
return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/