zoukankan      html  css  js  c++  java
  • Palindrome poj3974

    Palindrome
    Time Limit: 15000MS   Memory Limit: 65536K
    Total Submissions: 3280   Accepted: 1188

    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
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 using namespace std;
     5 char a[2100000];
     6 int p[21000000],t;
     7 int fun()
     8 {
     9     int i,r=0,c=0,max=0;
    10     p[0]=0;
    11     for(i=1;i<t;i++)
    12     {
    13         p[i]=r>i?(r-i<p[(c<<1)-i]?r-i:p[(c<<1)-i]):0;
    14         while(a[i+p[i]+1]==a[i-p[i]-1])p[i]++;
    15         max=max>p[i]?max:p[i];
    16         if(p[i]+i>r)
    17         {
    18             r=p[i]+i;
    19             c=i;
    20         }
    21     }
    22     return max;
    23 }
    24 int main()
    25 {
    26     char x;
    27     int tt=1;
    28     while(1)
    29     {
    30         t=0;
    31         a[t++]='#';
    32         while(x=getchar())
    33         {
    34             if(x=='
    ')break;
    35             a[t++]='&';
    36             a[t++]=x;
    37         }
    38         a[t++]='&';
    39         if(a[2]=='E')break;
    40         printf("Case %d: %d
    ",tt++,fun());
    41     }
    42 }
    View Code
  • 相关阅读:
    P4097 [HEOI2013]Segment 李超线段树
    P3592 [POI2015]MYJ
    P3698 [CQOI2017]小Q的棋盘
    P4098 [HEOI2013]ALO 可持久化01Trie
    P2331 [SCOI2005]最大子矩阵
    P4099 [HEOI2013]SAO
    loj #6032. 「雅礼集训 2017 Day2」水箱 线段树优化DP转移
    CF765F Souvenirs 离线+线段树+主席树
    CF1097D Makoto and a Blackboard
    loj #6570. 毛毛虫计数
  • 原文地址:https://www.cnblogs.com/ERKE/p/3597331.html
Copyright © 2011-2022 走看看