1297. Palindrome
Time limit: 1.0 second
Memory limit: 64 MB
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security service would have already started an undercover operation to establish the agent’s identity, but, fortunately, the letter describes communication channel the agent uses. He will publish articles containing stolen data to the “Solaris” almanac. Obviously, he will obfuscate the data, so “Robots Unlimited” will have to use a special descrambler (“Robots Unlimited” part number NPRx8086, specifications are kept secret).
Having read the letter, the “U.S. Robots” president recalled having hired the “Robots Unlimited” ex-employee John Pupkin. President knows he can trust John, because John is still angry at being mistreated by “Robots Unlimited”. Unfortunately, he was fired just before his team has finished work on the NPRx8086 design.
So, the president has assigned the task of agent’s message interception to John. At first, John felt rather embarrassed, because revealing the hidden message isn’t any easier than finding a needle in a haystack. However, after he struggled the problem for a while, he remembered that the design of NPRx8086 was still incomplete. “Robots Unlimited” fired John when he was working on a specific module, the text direction detector. Nobody else could finish that module, so the descrambler will choose the text scanning direction at random. To ensure the correct descrambling of the message by NPRx8086, agent must encode the information in such a way that the resulting secret message reads the same both forwards and backwards. In addition, it is reasonable to assume that the agent will be sending a very long message, so John has simply to find the longest message satisfying the mentioned property.
Your task is to help John Pupkin by writing a program to find the secret message in the text of a given article. As NPRx8086 ignores white spaces and punctuation marks, John will remove them from the text before feeding it into the program.
Input
The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters.
Output
The longest substring with mentioned property. If there are several such strings you should output the first of them.
Sample
input
Kazak
output
aza

问题链接:URAL1297 Palindrome
问题简述:(略)
问题分析
    该题可以算是计算字符串中最长回文的模板题,套manacher算法的函数计算就可以了。只是manacher算法的函数算得是最长回文长度,需要进一步计算回文字符串。
    需要注意的是使用的存储空间要合适。字符串长度为n时,需要2n+3的存储空间。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* URAL1297 Palindrome */#include <iostream>
#include <stdio.h>
#include <string.h>using namespace std;#define N 1000
char s[N + N + 3];
int p[N + N + 3];int manacher(char s[], int p[])
{int len = strlen(s), id = 0, maxlen = 0;for(int i = len; i >= 0; i--) {     /* 插入'#' */s[i + i + 2] = s[i];s[i + i + 1] = '#';}s[0] = '*';for(int i = 2; i < 2 * len + 1; i++) {if(p[id] + id > i)p[i] = min(p[2 * id - i], p[id] + id - i);elsep[i] = 1;while(s[i - p[i]] == s[i + p[i]])p[i]++;if(id + p[id] < i + p[i])id = i;maxlen = max(maxlen, p[i]);}return maxlen - 1;
}int main(void)
{int k, i;while(scanf("%s", s) != EOF) {k = manacher(s, p);for(i = 0; ; i++)if(p[i] - 1 == k)break;if(k & 1)i -= (k / 2) * 2;elsei -= (k / 2) * 2 - 1;for(; k--; i += 2)putchar(s[i]);putchar('\n');}return 0;
}

URAL1297 Palindrome【manacher算法】相关推荐

  1. 通俗易懂的最长回文串图解、说明及Java代码(中心扩散法和Manacher算法)

    1. 回文串 作为程序员,回文串这个词已经见怪不怪了,就是一个字符串正着读和反着读是一样的,形式如abcdcba.bbaabb.这里涉及到奇回文和偶回文,奇回文指回文串的字符数是奇数,偶回文指回文串的 ...

  2. java 最长回文串_通俗易懂的最长回文串图解、说明及Java代码(中心扩散法和Manacher算法)...

    1. 回文串 作为程序员,回文串这个词已经见怪不怪了,就是一个字符串正着读和反着读是一样的,形式如abcdcba.bbaabb.这里涉及到奇回文和偶回文,奇回文指回文串的字符数是奇数,偶回文指回文串的 ...

  3. manacher算法----O(n)最长回文串

    manacher算法----O(n)最长回文串 分类:字符串 (126)  (0)  举报  收藏 manacher的时间复杂度为O(n),后缀数组好像可以处理O(nlogn),但是有些变态题目可能卡 ...

  4. 谈谈我对Manacher算法的理解

    Manacher算法其实是求字符串里面最长的回文. ①在学习该算法前,我们应该知道回文的定义:顺序读取回文和逆序读取回文得到的结果是一样的,如:abba,aba. 那么我们不难想到,在判断一个字符串s ...

  5. 【字符串】manacher算法

    Definition 定义一个回文串为从字符串两侧向中心扫描时,左右指针指向得字符始终相同的字符串. 使用manacher算法可以在线性时间内求解出一个字符串的最长回文子串. Solution 考虑回 ...

  6. Manacher算法 , 实例 详解 . NYOJ 最长回文

    51 Nod http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 Manacher 算法 定义数组 p[i]表示以i为 ...

  7. (回文串)Manacher算法

    (回文串)Manacher算法 标签: ACM 回文串 Manacher 问题描述: 输入一个字符串,求出其中最大的回文子串.子串的含义是:在原串中连续出现的字符串片段.回文的含义是:正着看和倒着看相 ...

  8. hdu 3068 最长回文(manacher算法)

    最长回文                                                                         Time Limit: 4000/2000 M ...

  9. Manacher 算法模板

    简介 在字符串的题目中,有时会遇上 回文串 这样一个名词 顾名思义,回文串 就是 正读和反读都一样的字符串 而 最长回文子串 ,就是在一个字符串的所有子串中,是回文串且长度最长的那个 求最长回文子串最 ...

最新文章

  1. python语言程序设计基础网课-程序设计基础(Python语言)答案
  2. VTK:网格之QuadricDecimation
  3. Spring整合Redis做数据缓存(Windows环境)
  4. cad渐开线齿轮轮廓绘制_如何在机械CAD软件中自动生成齿轮
  5. 通过js滚轮滚动时调用动画_WOW.js在页面滚动时展现动感的元素动画效果
  6. 作业6--第3、4、5天进度
  7. ubuntu18.04 端口转发工具 Rinetd
  8. SERVICE_UNAVAILABLE/1/state not recovered / initialized
  9. 数字序号的级别与文章层次结构的关系
  10. Qt使用qwtplot3d绘制3D曲面
  11. SIMetrix教程-008.死区时间;Dead time
  12. 第十四章 字符编码(补充)
  13. 基于Python的经纬度与xy坐标系相互转换
  14. 和谁在一起,的确很重要
  15. 晋商消费金融总裁惠康获准,前不久被央行处罚49万元
  16. C#跨线程调用窗体控件的问题
  17. 有人说越来越多的大学生沉溺于游戏,为什么?
  18. 什么是信用评级?信用评级你了解多少?
  19. 0patch接棒微软,将为Win7提供安全补丁至2025年1月
  20. SVM转化为对偶问题求解的原因

热门文章

  1. ArcGIS Pro快速汉化方法-汉化GP
  2. 使用python制作ArcGIS插件(2)代码编写
  3. C#正则表达式——网游角色起名仅允许汉字、字母、数字、底划线
  4. 开发Adobe AIR移动应用程序的考虑事项
  5. linux 内核文件操作,Linux 内核文件操作
  6. ue4蓝图运行顺序_UE4蓝图解析(四)
  7. java判断光标位置_Java如何知道光标的当前位置?
  8. linux c取网卡名称,在Linux下用c编程肿么获取网卡序列号和硬盘序列号
  9. Python基础——try(异常处理)
  10. Bug(四)——error LNK1112:模块计算机类型x86与目标计算机类型x64冲突