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 }
  • 相关阅读:
    windows程序中的数据绑定
    dbhelper
    数据库错题
    构建布局良好的windows程序
    初始windows程序
    asp.net 文件下载 解决文件名乱码
    asp.net 文件下载 文件名称乱码 处理~~
    ASP.NET上传文件并记录到数据库
    Jquery配合Asp.Net无刷新删除指定服务器上的文件!
    Frame、Iframe、Frameset 的区别
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9507156.html
Copyright © 2011-2022 走看看