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. shopnc导入商品到大商创

    <?php //select member_name user_name,member_mobile mobile_phone,member_email email,member_passwd ...

  2. 利用tween,使用原生js实现模块回弹动画效果

    最近有一个需求,就是当屏幕往下一定像素时,下方会有一个隐藏的模块马上显现出来,向上运动后带有回弹效果.然后屏幕滚回去时这个模块能够原路返回 其实这个效果css3就可以很轻松实现,但是公司要求最低兼容i ...

  3. php函数、php定义数组和数组遍历

    <?php //php函数//1.简单函数//四要素:返回类型,函数名,参数列表,函数体 /*function Show(){ echo "hello";} Show();* ...

  4. spring MVC配置详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  5. windows nslookup、tracert 常用命令

    nslookup www.baidu.com 可以指定查询的类型,可以查到DNS记录的生存时间还可以指定使用哪个DNS服务器进行解释. tracert www.baidu.com 路由

  6. Mysql----------的一些常用命令

    1.查询一张表中某个字段重复值的记录 select id,cert_number from (select id,cert_number,count(*)as n from 表明 group by c ...

  7. IIS7 应用程序池设置成 经典 v2.0

    HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipe ...

  8. Coursera台大机器学习基础课程学习笔记2 -- 机器学习的分类

    总体思路: 各种类型的机器学习分类 按照输出空间类型分Y 按照数据标记类型分yn 按照不同目标函数类型分f 按照不同的输入空间类型分X 按照输出空间类型Y,可以分为二元分类,多元分类,回归分析以及结构 ...

  9. php中curl模拟浏览器来传输数据

    cURL可以使用URL的语法模拟浏览器来传输数据, 因为它是模拟浏览器,因此它同样支持多种协议,FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以 ...

  10. win7 64+python2.7.12安装numpy+scipy+matplotlib+scikit-learn

    python包下载网址 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 1.已经安装python2.7.12,查看scripts里是否有pip.2.7.9版本以上 ...