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

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

Source

关于manacher算法..........()

代码:

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define maxn 1000010
char ss[maxn];
char str[maxn << 1L];
int var[maxn << 1L]; int manacher() { int len = , mx_l = , pos = ;
str[] = '$';
str[] = '#';
for (int i = ; ss[i] != '\0'; i++) {
str[len++] = ss[i];
str[len++] = '#';
}
str[len] = '\0';
for (int i = ; i<len; i++) {
var[i] = mx_l>i ? std::min(var[ * pos - i], mx_l - i) : ;
while (str[i + var[i]] == str[i - var[i]])++var[i];
if (mx_l<i + var[i]) {
mx_l = i + var[i];
pos = i;
}
}
return len;
} int main() { int cnt = , len = , res = ;
while (scanf("%s", ss) != EOF) { if (ss[] == 'E') break;
len = manacher();
res = ;
for (int i = ; i<len; i++)
res =std::max(res, var[i]);
memset(str, , sizeof(char)*len);
printf("Case %d: %d\n", cnt++, res-);
}
return ;
}

POJ----(3974 )Palindrome [最长回文串]的更多相关文章

  1. Palindrome - POJ 3974 (最长回文子串,Manacher模板)

    题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了.   代码如下: ================================================= ...

  2. [LeetCode] Longest Palindrome 最长回文串

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  3. Longest Palindrome 最长回文串问题

    1.题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...

  4. 409 Longest Palindrome 最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串.注意:假设字符串的长度不会超过 ...

  5. LeetCode409Longest Palindrome最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...

  6. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

  7. Java实现 LeetCode 409 最长回文串

    409. 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意 ...

  8. Manacher算法 - 求最长回文串的利器

    求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...

  9. ACM题目————最长回文串

    Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等   Input 输入有多组cas ...

随机推荐

  1. 深入浅出Docker(一):Docker核心技术预览

    Docker是PaaS供应商dotCloud开源的一个基于LXC 的高级容器引擎,源代码托管在 GitHub 上, 基于Go语言开发并遵从Apache 2.0协议开源.Docker提供了一种在安全.可 ...

  2. linux pptpd账号同时登录的问题

    最近搞了个云主机搭建个VPN服务器给自己用, 特别是在公共场所的wifi上网时, 很多APP, 或者网站是没有https的, 所以为了保证信息(主要是账号密码)的安全, 搭个私有vpn还是很有必要的. ...

  3. Nginx下配置SSL安全协议

    生成证书: # cd /usr/local/nginx/conf # openssl genrsa -des3 -out server.key 1024 # openssl req -new -key ...

  4. tomcat 设置集群

    本文介绍的是使用tomcat内置的集群功能.跟官方文档的区别是没有使用广播,而是使用了static membership的方式. 需要修改server.xml 放在哪个元素下来的,是Host还是啥记不 ...

  5. [Android Tips] 14. Using Proguard with Android without obfuscation

    Option -dontobfuscate REF Using Proguard with Android without obfuscation

  6. Java基础之扩展GUI——高亮元素、上下文菜单、移动旋转元素、自定义颜色(Sketcher 10)

    窗口应用程序. 本例在上一版的基础上实现了高亮元素.移动元素.上下文菜单.旋转元素.设置自定义颜色. 1.自定义常量包: // Defines application wide constants p ...

  7. Asp.Net alert 方法

    public static void ExcuteAlert(Page page, string strAlerts)        {            ClientScriptManager  ...

  8. Spring 计划

    3.0----------------------------------------------------- SCRUM 流程的步骤2: Spring 计划 1. 确保product backlo ...

  9. SQL内联多个表

    SQL多个表组合成一个表: strSql.Append(@"Select N.NotificationOptionId, S.FullName, No.Title, N.SortCode, ...

  10. python 清楚数组重复字符串元素

    l1 = ['bb','c','d','bb','c','a','a'] l2 = {}.fromkeys(l1).keys() print (l2)