zoukankan      html  css  js  c++  java
  • Palindrome

    题目描述

    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.

    输入格式

    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). 

    输出格式

    For each test case in the input print the test case number and the length of the largest palindrome. 

    样例输入输出

    输入

    abcbabcbabcba
    abacacbaaaab
    END

    输出

    Case 1: 13
    Case 2: 6

    求最长回文子串的长度......
    一下是马拉车算法。
     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <cstdio>
     5 using namespace std ;
     6 
     7 char a[1100003] , b[2200006] ;
     8 int len[2200006] ;
     9 
    10 int main (  ) {
    11     int tot = 0 ;
    12     while( scanf( "%s" , a ) ) {
    13         tot ++ ;
    14         string c = a ;
    15         if( c == "END" ) break ;
    16         b[0] = '@' ; int cd = strlen(a) ;
    17         for( int x = 1 ; x <= cd*2 ; x+=2 ) {
    18             b[x] = '#' ;
    19             b[ x+1 ] = a[x/2] ;
    20         }    
    21         b[ cd*2+1 ] = '#' ;
    22         b[ cd*2+2 ] = '&' ;
    23         int  P = 0 , po = 0 , ans = 0 ;
    24         for( int i = 1 ; i <= cd*2+1 ; ++i ) {
    25             if( P >= i ) len[i] = min( P - i, len[ 2*po-i ] );
    26             else len[i] = 1 ;
    27             while( b[ i-len[i] ] == b[ i+len[i] ] )  ++ len[i]  ;  
    28             if( len[i] + i >= P ) {  
    29                 P = len[i] + i;  
    30                  po = i;  
    31          }
    32          ans = max( ans, len[i]);  
    33         } 
    34         printf( "Case %d: %d
    " , tot ,ans - 1 ) ;    
    35     }
    36     return 0 ;
    37 }
  • 相关阅读:
    js怎么通过逗号将string转换成数组
    设置mysql数据库为只读
    python 关于django 2.X from django.contrib.auth.views import login
    python Django2.X,报错 ‘learning_logs ’is not a registered namespace,如何解决?
    python django2.x报错No module named 'django.core.urlresolvers'
    python Django2.0如何配置urls文件
    VMware vSphere 组件和功能
    VMware vSphere Client的简单使用教程
    python 逻辑运算 ‘and’ ,'or' 在实战中的作用,代替if语句。
    python_urllib2:urlerror和httperror
  • 原文地址:https://www.cnblogs.com/Ateisti/p/5879090.html
Copyright © 2011-2022 走看看