zoukankan      html  css  js  c++  java
  • URAL 1297 Palindrome(Manacher)

    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):
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <iostream>
     5 using namespace std;
     6 typedef long long LL;
     7 
     8 const int MAXN = 2 * 1010;
     9 
    10 char str[MAXN], s[MAXN];
    11 int n, p[MAXN];
    12 
    13 void manacher() {
    14     int mx = 0, id;
    15     for(int i = 1; i < n; ++i) {
    16         if(mx > i) p[i] = min(p[2 * id - i], mx - i);
    17         else p[i] = 1;
    18         while(s[i + p[i]] == s[i - p[i]]) ++p[i];
    19         if(i + p[i] > mx) {
    20             id = i;
    21             mx = i + p[i];
    22         }
    23     }
    24 }
    25 
    26 void print() {
    27     int id = 0;
    28     for(int i = 1; i < n; ++i)
    29         if(p[i] > p[id]) id = i;
    30     int l = (id - p[id] + 1) / 2, r = l + p[id] - 1;
    31     for(int i = l; i < r; ++i) putchar(str[i]);
    32     puts("");
    33 }
    34 
    35 int main() {
    36     scanf("%s", str);
    37     s[n++] = '$', s[n++] = '#';
    38     for(int i = 0; str[i]; ++i) {
    39         s[n++] = str[i];
    40         s[n++] = '#';
    41     }
    42     s[n] = 0;
    43     manacher();
    44     print();
    45 }
    View Code
  • 相关阅读:
    leetcode1030之距离顺序排列矩阵单元格
    leetcode56之合并区间
    leetcode976之三角形最大周长
    leetcode922----按奇偶排序数组
    leetcode198之打家劫舍问题
    leetcode350之实现求解两数组交集(包含重复元素)
    【Python错误】日常记录(持续更新)
    【JavaScript】Lodash在React Native中的使用
    【Python】BeautifulSoup的使用
    转载【Python】python正则表达式详解
  • 原文地址:https://www.cnblogs.com/oyking/p/3536396.html
Copyright © 2011-2022 走看看