zoukankan      html  css  js  c++  java
  • poj 3974 Palindrome (manacher)

    Palindrome
    Time Limit: 15000MS   Memory Limit: 65536K
    Total Submissions: 12616   Accepted: 4769

    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

    C/C++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <vector>
     5 #include <string>
     6 #include <cstdio>
     7 #include <cstring>
     8 #include <climits>
     9 #include <iostream>
    10 #include <algorithm>
    11 #define INF 0x3f3f3f3f
    12 using namespace std;
    13 const int MAX = 3e6 + 10;
    14 
    15 char my_str[MAX], my_temp[MAX];
    16 int my_len1, my_len2, my_ans[MAX];
    17 
    18 void manacher()
    19 {
    20     int my_right = 0, my_mid = 0;
    21     for (int i = 1; i < my_len2; ++ i)
    22     {
    23         if (my_right > i) my_ans[i] = min(my_right - i, my_ans[(my_mid << 1) - i]);
    24         else my_ans[i] = 1;
    25         while (my_temp[i - my_ans[i]] == my_temp[i + my_ans[i]]) my_ans[i] ++;
    26         if (my_right < my_ans[i] + i)
    27         {
    28             my_right = my_ans[i] + i;
    29             my_mid = i;
    30         }
    31     }
    32 }
    33 
    34 int main()
    35 {
    36     int t = 1;
    37     while (scanf("%s", my_str), strcmp(my_str, "END"))
    38     {
    39         my_len1 = strlen(my_str);
    40         my_temp[0] = '$';
    41         my_temp[1] = '#';
    42         for (int i = 0, j = 2; i < my_len1; ++ i, j += 2)
    43         {
    44             my_temp[j] = my_str[i];
    45             my_temp[j + 1] = '#';
    46         }
    47 
    48         my_len2 = (my_len1 << 1) + 2;
    49         my_temp[my_len2] = '*';
    50 
    51         manacher();
    52         int temp = 0;
    53         for (int i = 1; i < my_len2; ++ i)
    54             if (temp < my_ans[i]) temp = my_ans[i];
    55         printf("Case %d: %d
    ", t ++, temp - 1);
    56     }
    57 
    58     return 0;
    59 }
  • 相关阅读:
    Java找N个数中最小的K个数,PriorityQueue和Arrays.sort()两种实现方法
    POJ 1661 Help Jimmy(C)动态规划
    LeetCode第8场双周赛(Java)
    Eclipse访问外部网站(比如:CSDN首页)
    LeetCode第151场周赛(Java)
    LeetCode第152场周赛(Java)
    Eclipse Block Selection(块选择)快捷键 Alt + Shift + A
    PAT(B) 1090 危险品装箱(Java)
    PAT(B) 1050 螺旋矩阵(Java:24分)
    PAT(B) 1045 快速排序(C)
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9507156.html
Copyright © 2011-2022 走看看