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 }
  • 相关阅读:
    Dubbo支持的协议的详解
    db2 SQL6036N解决办法
    9-5 HTTP注解演示及注意事项讲解
    9-4 Feign之HTTP注解介绍
    9-3 Feign演示及Feign注解解析
    9-2 Feign环境准备
    9-1 Feign自我介绍
    8-30 Hystrix章节总结
    8-29 实战技巧:如何设置线程池
    8-28 Hystrix监控讲解与演示
  • 原文地址:https://www.cnblogs.com/Ateisti/p/5879090.html
Copyright © 2011-2022 走看看