Palindrome

Time Limit: 15000MS Memory Limit: 65536K

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. 就是叫你去求一个字符串中的最长回文串,暴力肯定是会超时的,可以去看看manacher算法,这个算法不是很难。manacher详解。这就是一个manacher算法的模板题,看懂了就会做了。

#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e6+100;
char s[maxn<<1],ch[maxn<<1];
int rl[maxn<<1];
int get_ch(int len)
{
int ch_len = 0;
for(int i=0; i<len; i++)
{
ch[ch_len++] = '#';
ch[ch_len++] = s[i];
}
ch[ch_len++] = '#';//注意要把最后一个#给加上去
return ch_len;
} int manacher(int ch_len)
{
int max_right = 0,pos = 0,Max = 0;;
for(int i=0;i<ch_len;i++)
{
rl[i] = 1;
if(i < max_right)
rl[i] = min(rl[(pos<<1)-i],max_right-i);
while(i+rl[i]<ch_len && i-rl[i]>=0 && ch[i+rl[i]] == ch[i-rl[i]])
rl[i]++;
//max_right、pos更新
if(i + rl[i]-1 > max_right)
{
max_right = i+rl[i]-1;
pos = i;
}
//记录最大的那一个回文串
if(rl[i] > Max)
Max = rl[i];
}
return Max-1;//记得要减去1
} int main()
{
int t = 1;
while(scanf("%s",s) != EOF)
{
if(s[0] == 'E' && s[1] == 'N' && s[2] == 'D' && strlen(s) == 3)
break;
int len = strlen(s);
int len_ch = get_ch(len);//将元字符串改写
int ans = manacher(len_ch);
printf("Case %d: %d\n",t++,ans);
}
return 0;
}

字符串-POJ3974-Palindrome的更多相关文章

  1. [Swift]LeetCode680. 验证回文字符串 Ⅱ | Valid Palindrome II

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  2. poj3974 Palindrome【回文】【Hash】【二分】

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 13157   Accepted: 5028 Desc ...

  3. LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

  4. POJ----(3974 )Palindrome [最长回文串]

    Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 5121   Accepted: 1834 Description Andy ...

  5. POJ3974 Palindrome (manacher算法)

    题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...

  6. POJ3974 Palindrome

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  7. 【后缀数组】【线段树】poj3974 Palindrome

    考虑奇数长度的回文,对于字符串上的每个位置i,如果知道从i开始的后缀和到i为止的前缀反转后的字符串的lcp长度的话,也就知道了以第i个字符为对称中心的最长回文的长度了.因此,我们用在S中不会出现的字符 ...

  8. [POJ3974]Palindrome(后缀数组 || manacher)

    传送门 求一个串的最长回文子串的长度 1.后缀数组 把这个串反转后接到原串的后面,中间连一个没有出现过的字符. 然后求这个新字符串的某两个后缀的公共前缀的最大值即可. ——代码 #include &l ...

  9. [poj3974] Palindrome 解题报告 (hash\manacher)

    题目链接:http://poj.org/problem?id=3974 题目: 多组询问,每组给出一个字符串,求该字符串最长回文串的长度 数据范围支持$O(nlog n)$ 解法一: 二分+hash ...

  10. poj3974 Palindrome(Manacher最长回文)

    之前用字符串hash+二分过了,今天刚看了manacher拿来试一试. 这manacher也快太多了%%% #include <iostream> #include <cstring ...

随机推荐

  1. 如何使用Spring Security手动验证用户

    1.概述 在这篇快速文章中,我们将重点介绍如何以编程方式在Spring Security和Spring MVC中设置经过身份验证的用户. 2. Spring Security 简而言之,Spring ...

  2. 《javascript设计模式》笔记之第九章:组合模式

    之前一直都是按照书的结构顺序做总结,觉得好像不是很好,现在试着完全按照自己的理解做总结.例子还是书上的例子. 一:组合模式的作用: 在web开发中,主要用于创建嵌套的html结点,使得我们方便的把各种 ...

  3. CentOS 6.4 中yum命令安装php5.2.17

    最近给公司部署服务器的时候发现他们提供的服务器是centos6.4系统的,装好系统和相关服务httpd,mysql,php,一跑代码,发现php5.3中的zend加密不能用,安装Zend Guard ...

  4. c# 视频播放

    发表于: 2003-10-15 20:39:21 搞定了,嘿嘿!首先非常感谢zoujiaming在邮件中给我指了条路:用C#调用API搞定!!!使用的是mciSendString API函数主要参考了 ...

  5. shareTo 网页版分享

    // share -------- var shareTo = function (dest, shareCode) { var appKey = "1667889534"; // ...

  6. 编写C#程序,自动将bing首页图片设为壁纸

    任务目标: 1,获取图片 2,设为壁纸 3,自动化 环境需求: .NET Framework 4.0+, Visual Studio 2017 ==================== 1,获取图片 ...

  7. Bezier(贝塞尔曲线)

    CDC::PolyBezierBOOL PolyBezier( const POINT* lpPoints, int nCount ); 和 曲线原理及多段曲线连接处如何光滑连接:第一段曲线要有4个点 ...

  8. cookie存验证码时间,时间没走完不能再次点击

    <script> var balanceSeconds=getcookie('Num'); console.log(balanceSeconds) var timer; var isCli ...

  9. iOS开发笔记--关于 @synchronized,这儿比你想知道的还要多

    http://www.cocoachina.com/ios/20151103/14007.html 本文翻译自 Ryan Kaplan 的 More than you want to know abo ...

  10. GCD 代码以及GCD思想

    # 欧几里得算法 现在,我们来学习一下欧几里得算法. 欧几里得算法又称辗转相除法,主要用于算求两个正数之间的最大公约数.对于最大公约数这个名称,其英文名称为(Greatest Common Divis ...