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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<map>
#include<cstring>
#include<fstream>
using namespace std;
#define MAXN 1000003
typedef long long LL;
/*
最长回文串的长度
*/
char a[MAXN];
char s[MAXN*];
int r[MAXN*];
void Manacher(int len)
{
int p=;
s[p++] = '$';
s[p++] = '#';
for(int i=;i<len;i++)
{
s[p++] = a[i];
s[p++] = '#';
}
s[p] = ; int Mx = ,pos = ;
for(int i=;i<p;i++)
{
r[i] = (i<Mx)? min(Mx-i,r[*pos-i]):;
while(s[i+r[i]]==s[i-r[i]])
r[i]++;
if(i+r[i]>Mx)
{
Mx = i+r[i];
pos = i;
}
}
}
int main()
{
int cas = ;
while(scanf("%s",a),a[]!='E')
{
int l = strlen(a);
Manacher(l);
int ans = ;
for(int i=;i<*l+;i++)
{
ans = max(ans,r[i]-);
}
printf("Case %d: %d\n",cas++,ans);
}
}

U - Palindrome Manacher的更多相关文章

  1. hdu6230 Palindrome(manacher+树状数组)

    题目链接: Palindrome Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Other ...

  2. poj 3974 Palindrome (manacher)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 12616   Accepted: 4769 Desc ...

  3. POJ3974 Palindrome (manacher算法)

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

  4. ural 1297 Palindrome(Manacher模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...

  5. ●POJ 3974 Palindrome(Manacher)

    题链: http://poj.org/problem?id=3974 题解: Manacher 求最长回文串长度. 终于会了传说中的马拉车,激动.推荐一个很棒的博客:https://www.61mon ...

  6. HDU 3856 Palindrome ( Manacher + RMQ + 二分 ) WA!!!

    不知道错在哪了,求大神指教!!! 思路:用manacher求出每个以str[i]为中心轴的回文串的长度,RMQ预处理区间最大值,对于每个查询,二分最大回文串长,判定是否可行. #include < ...

  7. POJ3974 Palindrome Manacher 最长回文子串模板

    这道题可以$O(nlogn)$,当然也可以$O(n)$做啦$qwq$ $O(nlogn)$的思路是枚举每个回文中心,通过哈希预处理出前缀和后缀哈希值备用,然后二分回文串的长度,具体的就是判断在长度范围 ...

  8. Hdu-6230 2017CCPC-哈尔滨站 A.Palindrome Manacher 主席树

    题面 题意:给你一个字符串,问你满足s[i]=s[2n-i]=s[2n+i-2]的子串(这子串长度为3n-2)有多少个,原字符串长度<=5e5 题解:对于这种子串,其实要满足2个回文,跑过一次M ...

  9. poj3974 Palindrome(Manacher最长回文)

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

随机推荐

  1. 03_jni_helloworld_完成

    通过ndk-build编译C的代码.cd /d就是直接进到我的目录里面. 打开ANDROID-MK.HTML Introduction: This document describes the syn ...

  2. eclipse的快捷键---调试

    1:查看类或接口的方法 Ctrl+T 2:debug调试查看信息 Ctrl+Shift+i 3:debug调试快捷键 (1):F11好像是重新运行debug. (2):F8直接输出结果.(3):F5单 ...

  3. MySQL索引失效及使用索引的优缺点

    本文所有实验基于MySQL5.7.21,实验将会用到Explain工具,不了解的同学可参考此文章:MySQL性能优化神器Explain详解 联合索引失效 先创建一个包含三个字段的联合索引,索引顺序如下 ...

  4. 使用yum命令更新时锁住了怎么办?

    出现的状况如下: [root@iZwz951sp834mvbed8gdzzZ ~]# yum update kernelLoaded plugins: fastestmirrorExisting lo ...

  5. Log4J2的 PatternLayout

    Log4J2 PatternLayout 参考 日志样例 : 2018-10-21 07:30:05,184 INFO - DeviceChannelServiceImpl.java:434[defa ...

  6. Android_撕衣服小案例

    一直都觉得做安卓开发挺有意思,最近一段时间都在学习这方面的知识以及练习敲代码这次要说的是一个简单有趣的案例,相信大家也是看了标题才进来的吧,是不是有点迫不及待的想看看效果图,嘿嘿,算了还是直接给上源码 ...

  7. win7 硬盘安装suse双系统启动顺序更改

    使用win7硬盘安装suse双系统之后,首先面临的问题是,PC默认启动的系统更改的问题,有些人可能想默认启动是win7,只有在使用linux的时候在去选择suse系统,这里我告诉大家更改的办法: 首先 ...

  8. html——细线表格

    细线: 1.table表格设置背景色 2.table中设置单元格距离 3.tr标签设置另外一种背景色 <!DOCTYPE html> <html> <head lang= ...

  9. ASP.NET 缓存(Cache)

    ASP.NET提供了在一个ASP.NET应用程序基本上缓存信息的编程功能.该功能和Application对象相似,但它具有在ASP.NET应用程序的生命周期内动态维护缓存信息的能力.在应用程序中缓存数 ...

  10. 【译】x86程序员手册12-4.2系统指令

    4.2 Systems Instructions 系统指令 Systems instructions deal with such functions as: 系统指令具有以下功能: Verifica ...