HDU3294 Girls' research —— Manacher算法 输出解

题目链接:https://vjudge.net/problem/HDU-3294

 

Girls' research

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 3209    Accepted Submission(s): 1228


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
 

 

Recommend
lcy

 

 

题解:

求最长回文子串并输出解。此题的关键在于怎么记录子串的左右两端点。

根据Manacher算法所操作的字符数组Ma[]和原串s[],找出两者下标的对应关系即可。

 

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 typedef long long LL;
14 const double eps = 1e-6;
15 const int INF = 2e9;
16 const LL LNF = 9e18;
17 const int maxn = 2e5+10;
18 
19 char s[maxn], Ma[maxn<<1];
20 int Mp[maxn<<1];
21 int L, R;
22 
23 int Manacher(char *s, int len)
24 {
25     int ret = 0;
26     int l = 0;
27 
28     Ma[l++] = '$'; Ma[l++] = '#';
29     for(int i = 0; i<len; i++)
30     {
31         Ma[l++] = s[i];
32         Ma[l++] = '#';
33     }
34     Ma[l] = 0;
35 
36     int mx = 0, id = 0;
37     for(int i = 1; i<l; i++)
38     {
39         Mp[i] = mx>=i?min(Mp[2*id-i], mx-i):0;
40         while(Ma[i-Mp[i]-1]==Ma[i+Mp[i]+1]) Mp[i]++;
41         if(i+Mp[i]>mx)
42         {
43             mx = i+Mp[i];
44             id = i;
45         }
46         if(ret<Mp[i])
47         {
48             ret = Mp[i];
49             //第一步为求出在Ma数组上的位置, 第二步为求出在s数组,即在原串的位置
50             L = id-Mp[id]; L = (L+1)/2 - 1;
51             R = id+Mp[id]; R = R/2 - 1;
52         }
53     }
54     return ret;
55 }
56 
57 int main()
58 {
59     char turn;
60     while(scanf("%c", &turn)!=EOF)
61     {
62         scanf("%s", s); getchar();
63         int len = strlen(s);
64         for(int i = 0; i<len; i++)  //转换字符串
65         {
66             s[i] = s[i]-turn+'a';
67             if(s[i]<'a') s[i] += 26;
68         }
69 
70         int sum = Manacher(s, len);
71         if(sum<2)       //如果回文串长度小于二, 则无解
72             puts("No solution!");
73         else        //否则输出答案
74         {
75             printf("%d %d\n", L, R);
76             for(int i = L; i<=R; i++)
77                 putchar(s[i]);
78             putchar('\n');
79         }
80     }
81 }
View Code

 

posted on 2017-11-24 18:31  h_z_cong  阅读(451)  评论(0编辑  收藏  举报

导航