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 }
  • 相关阅读:
    字符编码 乱码问题
    Django ORM那些相关操作
    pymysql模块使用---Python连接MySQL数据库
    数据库MySQL 之 索引原理与慢查询优化
    数据库MySQL之 视图、触发器、存储过程、函数、事务、数据库锁、数据库备份、事件
    数据库 MySQL 之 数据操作
    数据库 MySQL 之 表操作、存储引擎
    [BZOJ 4212]神牛的养成计划(Trie+可持久化Trie)
    [LuoguP4094] [HEOI2016] [TJOI2016]字符串(二分答案+后缀数组+ST表+主席树)
    [BZOJ 2865]字符串识别(后缀数组+线段树)
  • 原文地址:https://www.cnblogs.com/sbwll/p/13364819.html
Copyright © 2011-2022 走看看