zoukankan      html  css  js  c++  java
  • POJ3974 Palindrome(Manacher模板题)

     POJ3974 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.InputYour 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).OutputFor each test case in the input print the test case number and the length o
    f the largest palindrome.
    给出一个长度为N(1 <= N <= 1000000)的字符串,求最长回文子串的长度。

    样例

    输入

    abcbabcbabcba
    abacacbaaaab
    END

    输出

    Case 1: 13
    Case 2: 6
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 char s[1000001],s_new[3000000];
     4 int p[3000000];
     5 int get()
     6 {
     7     s_new[0]='$';
     8     s_new[1]='#';
     9     int k=2;
    10     int len=strlen(s);
    11     for(int i=0;i<len;i++)
    12     {
    13         s_new[k++]=s[i];
    14         s_new[k++]='#';
    15     }
    16     s_new[k]='';
    17     return k;
    18 }
    19 int manacher()
    20 {
    21     int len=get();
    22     int id,mx=0;
    23     int minn=-1;
    24     for(int i=1;i<=len;i++)
    25     {
    26         if(i<mx)
    27         p[i]=min(mx-i,p[2*id-i]);
    28         else
    29         p[i]=1;
    30         while(s_new[i-p[i]]==s_new[i+p[i]])
    31         p[i]++;
    32         if(mx<i+p[i])
    33         {
    34             id=i;
    35             mx=i+p[i];
    36         }
    37         minn=max(minn,p[i]-1);
    38     }
    39     return minn;
    40 }
    41 int main()
    42 {
    43     int k=1;
    44     while(1)
    45     {
    46         cin>>s;
    47         if(s[0]=='E'&&s[1]=='N'&&s[2]=='D')
    48         break;
    49         cout<<"Case "<<k<<": "<<manacher()<<endl;
    50         k++;
    51     }
    52     return 0;
    53 }
  • 相关阅读:
    DataGridView重绘painting简单实例
    C#实现万年历(农历、节气、节日、星座、属相、生肖、闰年等)
    《开源框架那点事儿11》:软件开发杂谈
    半年总结——欲戴王冠,必承其重
    三天学会HTML5 之第一天
    读书笔记 -《高效程序猿的45个习惯-敏捷开发修炼之道》
    Opengl ES 1.x NDK实例开发之七:旋转的纹理立方体
    我与小娜(08):人工智能的伟大胜利
    阿里云 oss 小文件上传进度显示
    模仿猫眼电影App一个动画效果
  • 原文地址:https://www.cnblogs.com/sbwll/p/13364819.html
Copyright © 2011-2022 走看看