题目链接: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[],找出两者下标的对应关系即可。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int maxn = 2e5+; char s[maxn], Ma[maxn<<];
int Mp[maxn<<];
int L, R; int Manacher(char *s, int len)
{
int ret = ;
int l = ; Ma[l++] = '$'; Ma[l++] = '#';
for(int i = ; i<len; i++)
{
Ma[l++] = s[i];
Ma[l++] = '#';
}
Ma[l] = ; int mx = , id = ;
for(int i = ; i<l; i++)
{
Mp[i] = mx>=i?min(Mp[*id-i], mx-i):;
while(Ma[i-Mp[i]-]==Ma[i+Mp[i]+]) Mp[i]++;
if(i+Mp[i]>mx)
{
mx = i+Mp[i];
id = i;
}
if(ret<Mp[i])
{
ret = Mp[i];
//第一步为求出在Ma数组上的位置, 第二步为求出在s数组,即在原串的位置
L = id-Mp[id]; L = (L+)/ - ;
R = id+Mp[id]; R = R/ - ;
}
}
return ret;
} int main()
{
char turn;
while(scanf("%c", &turn)!=EOF)
{
scanf("%s", s); getchar();
int len = strlen(s);
for(int i = ; i<len; i++) //转换字符串
{
s[i] = s[i]-turn+'a';
if(s[i]<'a') s[i] += ;
} int sum = Manacher(s, len);
if(sum<) //如果回文串长度小于二, 则无解
puts("No solution!");
else //否则输出答案
{
printf("%d %d\n", L, R);
for(int i = L; i<=R; i++)
putchar(s[i]);
putchar('\n');
}
}
}

HDU3294 Girls' research —— Manacher算法 输出解的更多相关文章

  1. hdu3294 Girls' research manacher

    One day, sailormoon girls are so delighted that they intend to research about palindromic strings. O ...

  2. 算法进阶面试题01——KMP算法详解、输出含两次原子串的最短串、判断T1是否包含T2子树、Manacher算法详解、使字符串成为最短回文串

    1.KMP算法详解与应用 子序列:可以连续可以不连续. 子数组/串:要连续 暴力方法:逐个位置比对. KMP:让前面的,指导后面. 概念建设: d的最长前缀与最长后缀的匹配长度为3.(前缀不能到最后一 ...

  3. 【 HDU3294 】Girls' research (Manacher)

    BUPT2017 wintertraining(15) #5F HDU - 3294 题意 给定字母x,字符串变换一下: 'x'-1 -> 'z', 'x'->'a', 'x'+1-> ...

  4. Girls' research(manacher)

    Girls' research Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  5. Hdu 3294 Girls' research (manacher 最长回文串)

    题目链接: Hdu 3294  Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...

  6. 经典算法 Manacher算法详解

    内容: 1.原始问题   =>O(N^2) 2.Manacher算法   =>O(N) 1.原始问题 Manacher算法是由题目“求字符串中长回文子串的长度”而来.比如 abcdcb 的 ...

  7. [转] Manacher算法详解

    转载自: http://blog.csdn.net/dyx404514/article/details/42061017 Manacher算法 算法总结第三弹 manacher算法,前面讲了两个字符串 ...

  8. hdu3068之manacher算法+详解

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. Manacher算法详解

    问题 什么是回文串,如果一个字符串正着度读和反着读是一样的,这个字符串就被称为回文串. such as noon level aaa bbb 既然有了回文,那就要有关于回文的问题,于是就有了-- 最长 ...

随机推荐

  1. 查看Linux版本的方法

    1)命令: lsb_release -a [root@localhost tmp]# lsb_release -a LSB Version: :core-4.0-amd64:core-4.0-noar ...

  2. POJ1861 Network

    Time Limit: 1000MS   Memory Limit: 30000KB   64bit IO Format: %lld & %llu Description Andrew is ...

  3. PHP 之命名空间

    文件A.php namesspace a\b; Class User{ function get_user(){ echo 'this is A Class'; } } 文件B.php namessp ...

  4. Container With Most Water 双指针法

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  5. (5)ASP.NET Core 中的静态文件

    1.前言 当我们创建Core项目的时候,Web根目录下会有个wwwroot文件目录,wwwroot文件目录里面默认有HTML.CSS.IMG.JavaScript等文件,而这些文件都是Core提供给客 ...

  6. codevs科技庄园

    /* 因为每一秒只能走一个单位长度,而每走一个单位长度又会消耗一个体力值,如果体力值没有了时间还有也只能按照体力值计算,反之一样,所以V对于时间和体力值取小 cnt记录有桃子的树的个数,node[cn ...

  7. IntelliJ IDEA出现:This file is indented with tabs instead of 4 spaces的问题解决

    根据阿里巴巴Java开发手册,不能使用Tab字符,改成4个字符,设置如下: 注意:是不选择! 一定要选择这个:

  8. 深入GCD(二): 多核心的性能

    概念为了在单一进程中充分发挥多核的优势,我们有必要使用多线程技术(我们没必要去提多进程,这玩意儿和GCD没关系).在低层,GCD全局dispatch queue仅仅是工作线程池的抽象.这些队列中的Bl ...

  9. js可以控制文件上传的速度吗?

    为了减轻服务器负载,对于上传和下载的情况,我们需要进行流量控制,一般的方法是服务端做限流举措,比如很多ftp服务器,但是我想是不是可以使用前端js做呢? 顺着这个想法,我查了下资料,目前来看结论是No ...

  10. python 怎么启动一个外部命令程序, 并且不阻塞当前进程

    http://www.myexception.cn/perl-python/1278887.html http://blog.chinaunix.net/uid-25979788-id-3081912 ...