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. Jenkins+Gitlab+Ansible自动化部署(三)

    接Jenkins+Gitlab+Ansible自动化部署(一)https://www.cnblogs.com/zd520pyx1314/p/10210727.html 和(二)https://www. ...

  2. Exception sending context destroyed event to listener instance of class

    五月 29, 2019 6:29:39 下午 org.apache.catalina.core.StandardContext listenerStop严重: Exception sending co ...

  3. 《java学习二》jvm性能优化-----认识jvm

    Java内存结构 Java堆(Java Heap) java堆是java虚拟机所管理的内存中最大的一块,是被所有线程共享的一块内存区域. 在虚拟机启动时创建.此内存区域的唯一目的就是存放对象实例,这一 ...

  4. B. DZY Loves Modification

    B. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  5. EF 记录执行的sql语句

    最近做了个中等的项目,数据不会很多,开发时间比较紧迫,所以用了EF的框架. 在使用过程中,发现有时候执行的结果不如预期,想看看执行的sql语句为何,遍查找资料,在网上找到了相关辅助类,拿来使用,部署到 ...

  6. 9、调整数组顺序使奇数位于偶数前面------------>剑指offer系列

    题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 思路 首先寻找第一个 ...

  7. 使用PowerShell 获取azure image publisher offer sku 信息

    使用azure powershell 获取指定区域的可用镜像 publisher offer sku信息 param ( [parameter(Mandatory = $false)] $Locati ...

  8. EasyUI Tabs + Yii2.0实现iframe方式打开页面(解决共用静态文件引入加载的问题)

    在项目实际开发中,有将打开的各个链接页面隔离的需求(防止静态资源起冲突),这个时候常规思路就是使用iframe来实现!但遇到一个比较棘手的问题,当用easyui Tabs打开一个iframe页面时,怎 ...

  9. MySQL如何找出未提交事务信息

    前阵子,我写了一篇博客"ORACLE中能否找到未提交事务的SQL语句", 那么在MySQL数据库中,我们能否找出未提交事务执行的SQL语句或未提交事务的相关信息呢? 实验验证了一下 ...

  10. mongodb安全整理

    本文大都网上参考的,我只是整理了一下 一默认配置情况 1.MongoDB安装时不添加任何参数,默认是没有权限验证的,任何用户都可以登录进来,而且登录的用户可以对数据库任意操作而且可以远程访问数据库,需 ...