zoukankan      html  css  js  c++  java
  • Palindrome(最长回文串manacher算法)O(n)

     Palindrome
    Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    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<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 #include<algorithm>
     5 using namespace std;
     6 const int maxn=1000000+10;
     7 //manacher算法
     8 const int LEN=maxn;
     9 const int N=LEN*2;
    10 int p[N];
    11 char str[LEN],tmp[N];
    12 //p[i]表示以str[i]为中心的回文往右延伸的 最长长度
    13 void manacher(char* str, int* p)
    14 {
    15     int n=strlen(str), i, id, mx;
    16     for(p[mx=id=0]=i=1; i<n; i++)
    17     {
    18         p[i]=mx>i?min(p[2*id-i], mx-i+1):1;
    19         while(i+p[i]<n&&i-p[i]>=0&&str[i+p[i]]==str[i-p[i]]) p[i]++;
    20         if(mx<i+p[i]-1)
    21         {
    22             id=i;
    23             mx=i+p[i]-1;
    24         }
    25     }
    26 }
    27 //求str的最长回文的长度
    28 int maxPalindrome(char* str)
    29 {
    30     int ans=0;
    31     int i, n;
    32     for(i=0, n=strlen(str); i<n; i++)
    33     {
    34         tmp[2*i]='#';
    35         tmp[2*i+1]=str[i];
    36     }
    37     tmp[2*i]='#';
    38     tmp[2*i+1]='';
    39     n=n*2+1;
    40     manacher(tmp, p);
    41     for(i=0; i<n; i++)
    42     {
    43         if(ans < p[i]-1)
    44         {
    45             ans = p[i]-1;
    46         }
    47     }
    48     return ans;
    49 }
    50 int main()
    51 {
    52     int i=1;
    53     for(i=1;; i++)
    54     {
    55         scanf("%s",str);
    56         char s[]= {'E','N','D',''};
    57         if(strcmp(str,s)==0)
    58             break;
    59         else
    60             printf("Case %d: %d
    ",i,maxPalindrome(str));
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    【项目 · Wonderland】UML设计
    【项目 · Wonderland】预则立 && 他山之石
    【项目 · Wonderland】需求规格说明书 · 终版
    【项目 · 学生部门互选系统】项目展示
    【项目 · Wonderland】立项报告
    React 封装Form表单组件
    前端算法题解析 《四》
    前端算法题解析 《三》
    前端算法题解析 《二》
    前端算法题解析 《一》
  • 原文地址:https://www.cnblogs.com/wpnan/p/4093425.html
Copyright © 2011-2022 走看看