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.
 
题目大意:求最长回文子串并输出。
思路:Manacher算法的模板题,不多说。
 
代码(0.015S):
 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL; const int MAXN = * ; char str[MAXN], s[MAXN];
int n, p[MAXN]; void manacher() {
int mx = , id;
for(int i = ; i < n; ++i) {
if(mx > i) p[i] = min(p[ * id - i], mx - i);
else p[i] = ;
while(s[i + p[i]] == s[i - p[i]]) ++p[i];
if(i + p[i] > mx) {
id = i;
mx = i + p[i];
}
}
} void print() {
int id = ;
for(int i = ; i < n; ++i)
if(p[i] > p[id]) id = i;
int l = (id - p[id] + ) / , r = l + p[id] - ;
for(int i = l; i < r; ++i) putchar(str[i]);
puts("");
} int main() {
scanf("%s", str);
s[n++] = '$', s[n++] = '#';
for(int i = ; str[i]; ++i) {
s[n++] = str[i];
s[n++] = '#';
}
s[n] = ;
manacher();
print();
}

URAL 1297 Palindrome(Manacher)的更多相关文章

  1. Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  2. Ural 1297 Palindrome(后缀数组+最长回文子串)

    https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...

  3. URAL 1297 Palindrome(后缀数组+ST表)

    [题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新 ...

  4. Palindrome(Manacher)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 6183   Accepted: 2270 Descr ...

  5. 【学习笔记】字符串—马拉车(Manacher)

    [学习笔记]字符串-马拉车(Manacher) 一:[前言] 马拉车用于求解连续回文子串问题,效率极高. 其核心思想与 \(kmp\) 类似:继承. --引自 \(yyx\) 学姐 二:[算法原理] ...

  6. O(n)回文子串(Manacher)算法

    O(n)回文子串(Manacher)算法 资料来源网络 参见:http://www.felix021.com/blog/read.php?2040 问题描述: 输入一个字符串,求出其中最大的回文子串. ...

  7. URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...

  8. ural 1297 Palindrome(Manacher模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...

  9. Manacher Ural 1297 Palindrome

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

随机推荐

  1. 用java集合模拟登录和注册功能

    package com.linkage.login; import java.util.HashMap;import java.util.Iterator;import java.util.Map;i ...

  2. About Me - 关于

    0x00 简介 97年生 计算机相关专业,无线电安全攻防方向. 涉猎较多,喜欢研究无线.硬件.软件.网络.攻击.检测.防御等各类安全技术 精通较少,主要擅长的还是硬件.渗透.无线攻击方面. 现阶段在研 ...

  3. CF1066CBooks Queries(数组的特殊处理)

    题意描述 您需要维护一个数据结构,支持以下三种操作: L id:在现在序列的左边插一个编号为id的物品 R id:在现在序列的右边插一个编号为id的物品 ? id:查询该点左面有几个元素,右面有几个元 ...

  4. MYSQL 8.0.11 安装过程及 Navicat 链接时遇到的问题

    参考博客:https://blog.csdn.net/WinstonLau/article/details/78666423 我的系统和软件版本是这样的: 系统环境:win7.64位 MySQL版本: ...

  5. jQuery获取Select option 选择的Text和 Value

    获取一组radio被选中项的值:var item = $('input[name=items][checked]').val();获取select被选中项的文本var item = $("s ...

  6. PHP环境配置:Windows7+IIS7+PHP+MySQL - 适用于(2008 R2 / 8 / 10)

    配置需求 操作系统:Windows7(x32/x64), windows2008 IIS版本:7.0 PHP版本:7.0.6 及以上 MySQL版本:5.7.12 及以上 第一步:安装 IIS 注意: ...

  7. ThinkPHP5.0图片上传生成缩略图实例代码

    很多朋友遇到这样一个问题,图片上传生成缩略图,很多人在本机(win)测试成功,上传到linux 服务器后错误. 我也遇到同样的问题.网上一查,有无数的人说是服务器临时文件目录权限问题. 几经思考后,发 ...

  8. rails 启动测试环境出现 "Rack::Cors" => Rack::Cors,解决方法

    找到项目中"Rack::Cors"改为 Rack::Cors

  9. Python 爬虫 (三)

    #对第一章的百度翻译封装的函数进行更新 1 from urllib import request, parse from urllib.error import HTTPError, URLError ...

  10. rtsp over tcp并设置多个options

    版权声明:本文为博主原创文章,未经博主允许不得转载. var vlc=document.getElementById("vlc"); var options = new Array ...