zoukankan      html  css  js  c++  java
  • POJ3974 Palindrome

    Time Limit: 15000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

    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
     
    半夜刷题太容易犯蠢了……Case打成case,又WA掉了十五分钟人生
     
     1 /*by SilverN*/
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 const int mxn=1000100;
     9 char s[mxn],c[mxn<<1];
    10 int len;
    11 int p[mxn<<1];
    12 void mana(){
    13     int id=0,mx=0;
    14     int i,j;
    15     for(i=1;i<len;i++){
    16         if(mx>i)p[i]=min(p[2*id-i],p[id]+id-i);
    17         else p[i]=1;
    18         while(c[i+p[i]]==c[i-p[i]])p[i]++;
    19         if(p[i]+i>mx){
    20             mx=p[i]+i;
    21             id=i;
    22         }
    23     }
    24     return;
    25 }
    26 int main(){
    27     int cas=0;
    28     while(scanf("%s",s)!=EOF){
    29         if(s[0]=='E')break;
    30         int i,j;
    31         c[0]='$';c[1]='#';
    32         len=strlen(s);
    33         for(i=0;i<len;i++){
    34           c[i*2+2]=s[i];c[i*2+3]='#';
    35         }
    36         
    37         len=len*2+2;
    38         c[len]='%';
    39         mana();
    40         int ans=0;
    41         for(i=0;i<len;i++)ans=max(ans,p[i]);
    42         printf("Case %d: %d
    ",++cas,ans-1);
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    Sqlserver中一直在用又经常被忽略的知识点一
    PowerDesigner从Sqlserver中反转为带注释的字典及快捷键操作
    10.5 搜索的优化版
    每篇半小时1天入门MongoDB——1. MongoDB介绍和安装
    ASP.NET MVC搭建项目后台UI框架—11、自动加载下拉框查询
    Web项目从Oracle转为Mysql,fluentnhibernate-1.0和NHibernate2.1.0升级到NHibernate3.3的注意事项
    Mysql性能优化三(分表、增量备份、还原)
    Mysql性能优化二
    Mysql性能优化一
    Redis主从复制
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5870272.html
Copyright © 2011-2022 走看看