• 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 }
  • 相关阅读:
    SpringBoot常用注解(二)
    SpringBoot常用注解(一)
    Spring Dl解释
    Spring 入门程序
    Spring 入门
    JUnit-4.13使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误
    测试中Android与IOS分别关注的点
    python包中__init__.py的作用
    python自动发送测试报告(五)
    搭建一个有条理的项目(四)
  • 原文地址:https://www.cnblogs.com/Ateisti/p/5879090.html
Copyright © 2020-2023  润新知