URAL1297 Palindrome【manacher算法】

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

时间: 2024-10-13 03:33:55

URAL1297 Palindrome【manacher算法】的相关文章

POJ 3974 Palindrome Manacher算法题解

本题就是求最长的回文子串. 字符串超长,不过限时却是也很长的15秒,最长的限时之一题目了,如果限时短点的话,估计能过的人不多. 使用Mancher算法是可以秒杀的. 模板式的Manacher算法: #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #inc

Palindrome(最长回文串manacher算法)O(n)

Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "

【URAL】1297 Palindrome【字符串--manacher算法】

传送门:Palindrome 题意 求最长回文字符串,在学manacher算法,所以用了manacher,看到网上好多题解使用后缀数组来做的. 思路 manacher算法,参考<ACM国际大学生程序设计竞赛 算法与实现>的板子,一开始我以为板子的manacher算法是错误的,然后上网看题解. 直到我看到 https://blog.csdn.net/u012717411/article/details/53363444 文章,我才知道其实人家是对的,只不过我没理解. manacher算法在O(N

【Manacher算法】poj3974 Palindrome

Manacher算法教程:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 模板题,Code 附带注释: 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 char b[10000001],a[10000001]; 6 char tmp[10000001]; 7 int n,f[100

【后缀数组|最长回文子串】URAL-1297 Palindrome

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".

hdu-3613 Best Reward (manacher算法)

Best Reward 题目链接 Description After an uphill battle, General Li won a great victory. Now the head of state decide to reward him with honor and treasures for his great exploit. One of these treasures is a necklace made up of 26 different kinds of gems

HDU 3616 Best Reward (Manacher算法 前缀回文+后缀回文)

Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 785    Accepted Submission(s): 338 Problem Description After an uphill battle, General Li won a great victory. Now the head of state

【转载】Manacher算法

本文原创:http://www.cnblogs.com/BigBallon/p/3816890.html只为了记录学习,不为抄袭!http://www.felix021.com/blog/read.php?2040 对于Manacher算法,主要的作用是用来求一个字符串的最长回文子串.这个算法的时间复杂度书线性的,即O(n)下面我分两个部分来讲1)预处理这个算法的精妙之处在于巧妙地避免了考虑回文子串的长度是奇数还是偶数(如果你还不知道什么是回文数,回文串,请自行baidu)在Manacher算法

HDU 3068 最长回文 (manacher算法)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9188    Accepted Submission(s): 3159 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组