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);
else
p[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;
else
i -= (k / 2) * 2 - 1;
for(; k--; i += 2)
putchar(s[i]);
putchar('\n');
}
return 0;
}
原文地址:https://www.cnblogs.com/tigerisland45/p/10355255.html