HDU 3294 (Manacher) Girls' research

变形的求最大回文子串,要求输出两个端点。

我觉得把‘b‘定义为真正的‘a‘是件很无聊的事,因为这并不会影响到最大回文子串的长度和位置,只是在输出的时候设置了一些不必要的障碍。

另外要注意一下原字符串s1中的字符在预处理以后的字符串s2中对应的坐标关系,这样输出的时候就可以照着这个关系转化。

轻松1A,嘿嘿

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int maxn = 200000 + 20;
 8 char s1[maxn], s2[maxn * 2], real;
 9 int p[maxn * 2];
10
11 void init(char s1[], char s2[])
12 {
13     int j = 2;
14     s2[0] = ‘$‘, s2[1] = ‘#‘;
15     for(int i = 0; s1[i] != ‘\0‘; ++i)
16     {
17         s2[j++] = s1[i];
18         s2[j++] = ‘#‘;
19     }
20     s2[j] = ‘\0‘;
21 }
22
23 void manacher(char s[])
24 {
25     p[0] = 0;
26     int id, mx = 0;
27     for(int i = 1; s[i] != ‘\0‘; ++i)
28     {
29         if(mx > i)
30             p[i] = min(p[id*2-i], mx-i);
31         else
32             p[i] = 1;
33         while(s[i + p[i]] == s[i - p[i]])
34             ++p[i];
35         if(i + p[i] > mx)
36         {
37             id = i;
38             mx = i + p[i];
39         }
40     }
41 }
42
43 char change(char c)
44 {
45     c -= real - ‘a‘;
46     if(c < ‘a‘)    c += 26;
47     if(c > ‘z‘)    c -= 26;
48     return c;
49 }
50
51 void getans(void)
52 {
53     int mlen = 1, pos;
54     for(int i = 1; s2[i] != ‘\0‘; ++i)
55     {
56         if(p[i] - 1 > mlen)
57         {
58             mlen = p[i] - 1;
59             pos = i;
60         }
61     }
62     if(mlen == 1)
63         printf("No solution!\n");
64     else
65     {
66         int L = (pos - (p[pos] - 2)) / 2 - 1;
67         int R = (pos + (p[pos] - 2)) / 2 - 1;
68         printf("%d %d\n", L, R);
69         for(int i = L; i <= R; ++i)
70             printf("%c", change(s1[i]));
71         printf("\n");
72     }
73 }
74
75 int main(void)
76 {
77     #ifdef LOCAL
78         freopen("3294in.txt", "r", stdin);
79     #endif
80
81
82     while(scanf("%c", &real) == 1)
83     {
84         scanf("%s", s1);
85         getchar();
86         init(s1, s2);
87         manacher(s2);
88         getans();
89     }
90     return 0;
91 }

代码君

HDU 3294 (Manacher) Girls' research

时间: 2024-08-05 10:47:29

HDU 3294 (Manacher) Girls' research的相关文章

hdu 3294 manacher 求回文串

感谢: http://blog.csdn.net/ggggiqnypgjg/article/details/6645824/ O(n)求给定字符串的以每个位置为中心的回文串长度. 中心思想:每次计算位置i的答案时,利用已经算出的1~i-1位置的答案. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define maxn 222222 5 using namespace std; 6 7

hdu 3294 manacher算法

没什么还说的  也就是这一类的裸题吧 #include<stdio.h> #include<string.h> #include<iostream> using namespace std; char str[200010],str1[400010]; int mark[400010]; int min(int a,int b) { return a<b?a:b; } int main() { char str2[5]; int i,j; while(~scanf

(回文串 Manacher )Girls&#39; research -- hdu -- 3294

http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3294 Description One day, sailormoon girls are so delighted that they intend to res

HDU 3294 Girls&#39; research (Manacher算法 + 记录区间)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3294 题目大意:输入一个字符ch和一个字符串,问如果把ch当作'a'的话,字符串的每个字符也要做相应变化,如b aa,若b为'a',则b前面的a就为'a'前面的'z',这里是循环表示,输出字符串的最长回文子串,如果最长回文子串串长为1,输出No solution! 几乎是模板题,唯一的特别之处就是要输出回文串字符,所以要记录max(Mp[i])对应的在原串中的字符区间,根据Manacher算法的步骤

Hdu 3294 Girls&#39; research (manacher 最长回文串)

题目链接: Hdu 3294  Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.......,a == z. 问最长回文串所在区间,以及最长回文串所表示的明码. 解题思路: 字符串长度[1,200000],用manacher算法很轻松就搞定了. get√新技能请点击me 1 #include <cstdio> 2 #include <cstring> 3 #includ

[manacher] hdu 3294 Girls&#39; research

题意: 给一个字符x代表真实的a 然后输出的时候转换 然后就是求最长回文子串的串是什么 长度要大于1 思路: 就是裸的manacher,弄清楚下标的转换关系就好了 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"queue" #include"algorithm" #inc

[manacher] hdu 3294 Girls&amp;#39; research

题意: 给一个字符x代表真实的a 然后输出的时候转换 然后就是求最长回文子串的串是什么 长度要大于1 思路: 就是裸的manacher,弄清楚下标的转换关系就好了 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"queue" #include"algorithm" #inc

HDU 3294 Girls&#39; research

题目地址 manacher 1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int Nmax=200005; 6 char s[Nmax]; 7 char str[Nmax*2+10]; 8 int p[Nmax*2+10]; 9 int hashh[Nmax*2+10]; 10 int id; 11 int maxlen; 12 i

Girls&#39; research HDU - 3294(马拉车水题)

题意: 求最长回文串 长度要大于等于2  且输出起点和终点  输出回文串字符 这个字符还是要以给出的字符为起点a 输出 解析: 分析一下s_new串就好了 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include &l