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.
样例输入
1b babd
2a abcd
样例输出
10 2
2aza
3No solution!
提示
无
思路
Manacher,字符置换不影响回文性质,先求出最长回文子串长度和起点,输出的时候替换一下即可。
代码
1char s[mxn], t[mxn];
2int p[mxn], l;
3
4int manacher_init(char *s, char *t, int n)
5{
6 int j = 2;
7 t[0] = -2, t[1] = -1;
8
9 for (int i = 0; i < n; i++)
10 {
11 t[j++] = s[i];
12 t[j++] = -1;
13 }
14 t[j] = -3;
15 return j;
16}
17
18int manacher(char *t, int *p, int n)
19{
20 int id = 0, mx = 0, ans = 0;
21 for (int i = 1; i <= n; i++)
22 {
23 p[i] = i<mx ? min(p[2*id-i], mx-i) : 1;
24
25 while (t[i+p[i]] == t[i-p[i]]) p[i]++;
26
27 if (mx < i+p[i])
28 mx = i+p[i], id = i;
29
30 if(ans < p[i]){
31 ans = p[i];
32 l = (i-p[i])/2;
33 }
34 }
35 return ans-1;
36}
37
38int main()
39{
40 char c;
41 while(~scanf(" %c %s", &c, s))
42 {
43 int n = manacher_init(s, t, strlen(s));
44 int ans = manacher(t, p, n);
45 if(ans>2){
46 printf("%d %d\n", l, l+ans-1);
47 for(int i=0; i<ans; i++){
48 printf("%c", (s[i+l]-c+26)%26+'a');
49 }
50 printf("\n");
51 }else{
52 printf("No solution!\n");
53 }
54 }
55 return 0;
56}