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 }
  • 相关阅读:
    实验一 总结
    C#中将JObject类型数据转换成这样的货币数字-带千分符-四舍五入2位小数
    Git常用命令+报错solution
    Python Requests学习笔记
    Python requests 环境搭建
    关于Page Object个人结合测试框架的一些理解
    REST理解 + API测试学习笔记
    记录组内做API测试的流程
    理解c#的Get Set访问器及测试脚本中的应用
    tp3
  • 原文地址:https://www.cnblogs.com/kirai/p/4769191.html
Copyright © 2011-2022 走看看