zoukankan      html  css  js  c++  java
  • [POJ3974]Palindrome

    题目链接:http://poj.org/problem?id=3974

    Description

    Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"

    A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.

    The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".

    If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

    Input

    Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity).

    Output

    For each test case in the input print the test case number and the length of the largest palindrome.

    Sample Input

    abcbabcbabcba
    abacacbaaaab
    END

    Sample Output

    Case 1: 13
    Case 2: 6

    Source

     
     
    Manacher,数组要开大一些,否则会有奇怪的RE
     
     
     
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <cmath>
     7 #include <queue>
     8 #include <map>
     9 #include <stack>
    10 #include <list>
    11 #include <vector>
    12 
    13 using namespace std;
    14 
    15 const int maxn = 2222222;
    16 int pre[maxn];
    17 char str[maxn];
    18 char tmp[maxn];
    19 
    20 inline int min(int x, int y) {
    21     return x < y ? x : y;
    22 }
    23 
    24 int init(char *tmp, char *str) {
    25     int len = strlen(str);
    26     tmp[0] = '$';
    27     for(int i = 0; i <= len; i++) {
    28         tmp[2*i+1] = '#';
    29         tmp[2*i+2] = str[i];
    30     }
    31     len = 2 * len + 2;
    32     tmp[len] = 0;
    33     return len;
    34 }
    35 
    36 void manacher(int *pre, char *tmp, int len) {
    37     int id = 0;
    38     int mx = 0;
    39     for(int i = 1; i < len; i++) {
    40         pre[i] = mx > i ? min(pre[2*id-i], mx-i) : 1;
    41         while(tmp[i+pre[i]] == tmp[i-pre[i]]) {
    42             pre[i]++;
    43         }
    44         if(pre[i] + i > mx) {
    45             id = i;
    46             mx = pre[i] + id;
    47         }
    48     }
    49 }
    50 
    51 int main() {
    52     int kase = 1;
    53     // freopen("in", "r", stdin);
    54     while(~scanf("%s", str) && strcmp(str, "END") != 0) {
    55         memset(pre, 0, sizeof(pre));
    56         memset(tmp, 0, sizeof(tmp));
    57         int len = init(tmp, str);
    58         manacher(pre, tmp, len);
    59         int ans = 1;
    60         for(int i = 0; i < len; i++) {
    61             ans = max(ans, pre[i]);
    62         }
    63         printf("Case %d: %d
    ", kase++, ans - 1);
    64     }
    65 }
  • 相关阅读:
    招行面试
    今日头条面试[教育岗]
    四方精创 面试
    ArrayList 源码
    redis缓存,穿透,击穿,雪崩
    hashMap
    集合整理
    阿里CBU技术部一面
    网安面试
    php递归获取顶级父类id
  • 原文地址:https://www.cnblogs.com/kirai/p/4769191.html
Copyright © 2011-2022 走看看