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

  1. abcbabcbabcba
  2. abacacbaaaab
  3. END

Sample Output

  1. Case 1: 13
  2. Case 2: 6

Source

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

代码:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<algorithm>
  5. #define maxn 1000010
  6. char ss[maxn];
  7. char str[maxn << 1L];
  8. int var[maxn << 1L];
  9.  
  10. int manacher() {
  11.  
  12. int len = , mx_l = , pos = ;
  13. str[] = '$';
  14. str[] = '#';
  15. for (int i = ; ss[i] != '\0'; i++) {
  16. str[len++] = ss[i];
  17. str[len++] = '#';
  18. }
  19. str[len] = '\0';
  20. for (int i = ; i<len; i++) {
  21. var[i] = mx_l>i ? std::min(var[ * pos - i], mx_l - i) : ;
  22. while (str[i + var[i]] == str[i - var[i]])++var[i];
  23. if (mx_l<i + var[i]) {
  24. mx_l = i + var[i];
  25. pos = i;
  26. }
  27. }
  28. return len;
  29. }
  30.  
  31. int main() {
  32.  
  33. int cnt = , len = , res = ;
  34. while (scanf("%s", ss) != EOF) {
  35.  
  36. if (ss[] == 'E') break;
  37. len = manacher();
  38. res = ;
  39. for (int i = ; i<len; i++)
  40. res =std::max(res, var[i]);
  41. memset(str, , sizeof(char)*len);
  42. printf("Case %d: %d\n", cnt++, res-);
  43. }
  44. return ;
  45. }

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. 淘宝开放平台Session Key有效期

    各标签session时长及RefreshToken失效时长  *Refresh失效时长为0,即该sessionkey不可刷新. 标签 授权时长 Refresh失效时长 商家后台应用 固定时长1年 0 ...

  2. LUA OOP 单例模式实现的 一个 方案

    单例 存在这么一类class, 无论class怎么初始化, 产生的instance都是同一个对象. Code string.toHTMLCode = function(self) return enc ...

  3. RDIFramework.NET ━ 9.12 表字段管理 ━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.12  表字段管理 -Web部分  表(字段)综合管理模块主要功能是对数据表本身,表的各个字段做相应的控制,具体有以下功能: ...

  4. WkHtmlToPdf 生成 PDF

    1. 首先去http://wkhtmltopdf.org/downloads.html 下载最新版本的安装包 2. 执行安装完成 3. CMD 命令行运行wkhtmltopdf.exe程序生成PDF ...

  5. 使用NetBeans搭建基于Spring框架的Web应用

    NetBeans下载链接:https://netbeans.org/. 第一步:选择“文件”菜单下的“新建项目”: 第二步:类别选择“Java Web”,项目选择“Web应用程序”,单击“下一步”: ...

  6. bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群

    网络流,对于每一个行星,将行星所在行到行星连一条流量为1的边,将行星到其所在列连一条流量为1的边,从源点到所有行连一条流量为1的边,将所有列到汇点都连一条流量为1的边,最大流即为答案. 代码 #inc ...

  7. Android PullToRefresh (ListView GridView 下拉刷新) 使用详解

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38238749,本文出自:[张鸿洋的博客] 群里一哥们今天聊天偶然提到这个git ...

  8. 递归函数 Python

    函数: def fact(n): if n==1: return 1 return n * fact(n-1) 递归过程: print(fact(5)) >>fact(5) >> ...

  9. 多浏览器兼容用javascript获取url参数的方法比较推荐的一种

    多浏览器兼容用javascript获取url参数的方法比较推荐的一种 <script language = javascript> function request(paras){ var ...

  10. this的面面观

    http://www.cnblogs.com/Wayou/p/all-this.html <JavaScript语言精粹> 全局this 浏览器宿主的全局环境中, function f(x ...