http://poj.org/problem?id=3974
Palindrome
Time Limit: 15000MSMemory Limit: 65536K
Total Submissions: 2707Accepted: 995
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

Seventh ACM Egyptian National Programming Contest
解析:
题意:求最长回文串长度:
思路:
利用manacher算法计算;
这里简要概述一下这个算法;
总的来说是相邻字符中间插入一个相同的字符构成一新的字符(以此可以避免奇偶性讨论。),然后求以每个点为中心的回文字长度。
就这以下标号来解释一下:
p[i]:以第i个字符为中心的回文长度;
id:前一个回文中心.
mx:前一个回文的最有右端 ,mx=p[id]+id;
1.逐次枚举i判断是否在mx前面,更新p[i]初值;
2.左右看扩展,求当前最大的p[i];
3.更新  id,mx;
4得出的结果为ans=max{p[i]-1};
Accepted580K 47MSG++ 740B
*/

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn=1000000+10;
  7. char s[maxn],st[maxn*2];
  8. int p[maxn*2];
  9. void manacher()
  10. {
  11. int i,j,len;
  12. len=strlen(s);
  13. for(i=0,j=0;i<len;i++,j+=2)//构造新数组
  14. {
  15. st[j]='#';
  16. st[j+1]=s[i];
  17. }
  18. st[2*len]='#';//末尾处不可忽略
  19. int mx=0,id;
  20. for(i=1;i<=2*len;i++)
  21. {
  22. if(mx>i)//如果此时的中心点仍在前一个回文串中
  23. {p[i]=min(p[2*id-i],mx-i);
  24. }
  25. else
  26. p[i]=1;
  27. //由中心向两边扩展
  28. for(;st[i-p[i]]==st[i+p[i]]&&(i-p[i]>=0)&&(i+p[i]<=2*len);p[i]++)
  29.  
  30. if(mx<i+p[i])//更新mx,id;
  31. {
  32. id=i;
  33. mx=i+p[i];
  34. }
  35. }
  36. int ans=0;
  37. for(i=1;i<=len*2;i++)//得到最长回文字
  38. {
  39. if(p[i]>ans)
  40. ans=p[i];
  41. }
  42. printf("%d\n",ans-1);
  43. }
  44. int main()
  45. { int ca=0;
  46. while(scanf("%s",s)!=EOF)
  47. {
  48. if(strcmp(s,"END")==0)
  49. break;
  50. printf("Case %d: ",++ca);
  51. manacher();
  52. }
  53. return 0;
  54. }

Palindrome(poj3974)(manacher算法)的更多相关文章

  1. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  2. POJ 3974 Palindrome 字符串 Manacher算法

    http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...

  3. POJ3974 Palindrome (manacher算法)

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

  4. 【Manacher算法】poj3974 Palindrome

    Manacher算法教程:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 模板题,Code 附带注释: #include<cs ...

  5. Palindrome(最长回文串manacher算法)O(n)

     Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  6. 利用Manacher算法寻找字符串中的最长回文序列(palindrome)

    寻找字符串中的最长回文序列和所有回文序列(正向和反向一样的序列,如aba,abba等)算是挺早以前提出的算法问题了,最近再刷Leetcode算法题的时候遇到了一个(题目),所以就顺便写下. 如果用正反 ...

  7. hdu 3068 最长回文 manacher算法(视频)

    感悟: 首先我要Orz一下qsc,我在网上很难找到关于acm的教学视频,但偶然发现了这个,感觉做的很好,链接:戳戳戳 感觉这种花费自己时间去教别人的人真的很伟大. manacher算法把所有的回文都变 ...

  8. HDU3068 最长回文 Manacher算法

    Manacher算法是O(n)求最长回文子串的算法,其原理很多别的博客都有介绍,代码用的是clj模板里的,写的确实是异常的简洁,现在的我只能理解个大概,下面这个网址的介绍比较接近于这个模板,以后再好好 ...

  9. 【转载】Manacher算法

    本文原创:http://www.cnblogs.com/BigBallon/p/3816890.html只为了记录学习,不为抄袭!http://www.felix021.com/blog/read.p ...

随机推荐

  1. Asp.net MVC 视图之公用代码

    一.公共模板 转自:http://www.cnblogs.com/kissdodog/archive/2013/01/07/2848881.html 1.@RenderBody() 在网站公用部分通过 ...

  2. MySql 命令积累

    一. 修改表的自增起点 ; 二.获取自增键值 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID ...

  3. 164. Maximum Gap

    题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...

  4. RxJava开发精要8 – 与REST无缝结合-RxJava和Retrofit

    原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...

  5. 重温XML

    关于什么是XML,以及XML有什么作用,网络上很多,我就在这里不班门弄斧,写博客,是我的一个习惯,究其内容无非个人情感,心得体会,转载,技术相关的,或者一时心血来潮的个人之谈,但是我是一个小心翼翼的人 ...

  6. Spring cloud项目实践(一)

    链接地址:http://sail-y.github.io/2016/03/21/Spring-cloud%E9%A1%B9%E7%9B%AE%E5%AE%9E%E8%B7%B5/ 什么是Spring ...

  7. mysql导出csv/excel文件的几种方法,mysql的load导入csv数据

    方法一 php教程用mysql的命令和shell select * into outfile './bestlovesky.xls' from bestlovesky where 1 order by ...

  8. cat ,more, Less区别

    使用cat more less都可以查看文本内容,但是它们三者有什么区别呢?more和less的功能完全重复吗?以下是我个人的总结,欢迎大家一起来分享 cat        连续显示.查看文件内容mo ...

  9. SharePoint 2010 部署 WSP 包

    转:http://www.cnblogs.com/myheaven/archive/2011/05/19/2051180.html .net在工程的下面生成了WSP包,在Debug下面.需要放到生产环 ...

  10. Servlet3.0学习总结(三)——基于Servlet3.0的文件上传

    在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileupload组件,在Servlet3.0中提供了对文件上传的原生支持,我们不 ...