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. 使用Zeppelin时出现sh interpreter not found错误的解决办法(图文详解)

    不多说,直接上干货! 问题详解 http://192.168.80.145:8099/#/notebook/2CSV2VT5S 相关博客是 Zeppelin的入门使用系列之使用Zeppelin运行sh ...

  2. golang学习资料

    http://yougg.github.io/static/gonote/GolangStudy.html 

  3. Bootstrap下拉菜单相关

    1.实现普通下拉菜单:.dropdown>button.dropdown-toggle[data-toggle="dropdown"]+ul.dropdown-menu; 2 ...

  4. (2017.10.10) 我对 JavaScript 历史的认识

    关于 JavaScript 的历史和来由相信学过 JavaScript 的小伙伴都能说出一二.我看过一些入门书籍第一章或者前言部分都有介绍,但是一直没捋清这段历史.今天通过两个问题,来加深我对 Jav ...

  5. 如何真正解决“ UWP DEP0700: 应用程序注册失败。[0x80073CF9] 另一个用户已安装此应用的未打包版本。当前用户无法将该...”的问题

    http://www.cnblogs.com/hupo376787/p/8267796.html 谈到了解决该问题的临时方案,那如何真正的解决该问题 目测可以开启设备门户来删除包

  6. unbuntu&vim&Kali的各种小知识

    1. vmware workstation 15.0.0 2.ubuntu-18.10-desktop  使用网络地址转换 VMware workstation 1.ctrl+alt 返回  unbu ...

  7. FastText算法

    转载自: https://www.cnblogs.com/huangyc/p/9768872.html 0. 目录 1. 前言 2. FastText原理 2.1 模型架构 2.2 层次SoftMax ...

  8. ValidForm验证表单

    在做项目时,要求熟悉项目中验证表单的插件,所以学习一下validForm这个插件 http://validform.rjboy.cn/document.html#validformObject

  9. HTML5中Web存储

    HTML5 中web存储是一个比cookies更好的一个本地存储方式. 那么什么是HTML5存储呢? 使用HTML5可以在本地存储用户浏览的数据,HTML5技术没有出来之前是使用cookies进行本地 ...

  10. 12_1_Annotation注解

    12_1_Annotation注解 1. 什么是注解 Annotation是从JDK5.0开始引入的新技术. Annotation的作用: 不是程序本身,可以对程序作出解释.可以被其他程序(比如,编译 ...