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
  • 相关阅读:
    关于BigDecimal转String的准确性问题
    MyBatis动态Sql之if标签的注意事项
    Servlet与通信协议概述
    关于ThreadLocal的那些事
    MyBatis 中 @Param 注解的四种使用场景
    Mybatis中#{}与${}的区别
    如何重新加载 Spring Boot 上的更改,而无需重新启动服务器?
    jsp有哪些动作?作用分别是什么?
    forward 和redirect的区别 ?
    Eureka和ZooKeeper都可以提供服务注册与发现的功能,请说说两个的区别?
  • 原文地址:https://www.cnblogs.com/ERKE/p/3597331.html
Copyright © 2011-2022 走看看