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 }
  • 相关阅读:
    css中margin-left与left的区别
    Python文件和目录模块介绍:glob、shutil、ConfigParser
    [ Python入门教程 ] Python文件基本操作_os模块
    使用Pyinstaller转换.py文件为.exe可执行程序
    Windows命令行打开常用界面
    如何做好性能测试_流程篇
    Windows查看指定端口是否占用和查看进程
    ‘操作无法完成 ,因为其中的文件夹或文件已在另一程序中打开’问题解决
    bat脚本基础教程
    vi编辑器常用命令
  • 原文地址:https://www.cnblogs.com/sbwll/p/13364819.html
Copyright © 2011-2022 走看看