POJ   3974

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
  3.  
  4. 题意:给一个字符串求这个串的最长回文子串的长度;
  5.  
  6. 思路:用以下回文串的模板可以在线性时间内完成求最长的回文串的长度;
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstdio>
  4. #include<cstring>
  5. using namespace std;
  6. const int N=;
  7. int n,p[N];
  8. char s[N],str[N];
  9.  
  10. void kp()
  11. {
  12. int i;
  13. int mx=;
  14. int id;
  15. ///for(i=n;str[i]!=0;i++)
  16. ///str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba;
  17. for(i=;i<n;i++)
  18. {
  19. if(mx>i)
  20. p[i]=min(p[*id-i],p[id]+id-i);
  21. else
  22. p[i]=;
  23. for( ;str[i+p[i]]==str[i-p[i]];p[i]++);
  24. if(p[i]+i>mx)
  25. {
  26. mx=p[i]+i;
  27. id=i;
  28. }
  29. }
  30. }
  31.  
  32. void init()
  33. {
  34. str[]='$';
  35. str[]='#';
  36. for(int i=;i<n;i++)
  37. {
  38. str[i*+]=s[i];
  39. str[i*+]='#';
  40. }
  41. n=n*+;
  42. s[n]=;
  43. }
  44.  
  45. int main()
  46. {
  47. int Case=;
  48. while(scanf("%s",s)!=EOF)
  49. {
  50. if(s[]=='E'&&s[]=='N'&&s[]=='D')
  51. break;
  52. n=strlen(s);
  53. init();
  54. kp();
  55. int ans=;
  56. for(int i=;i<n;i++)
  57. if(p[i]>ans)
  58. ans=p[i];
  59.  
  60. printf("Case %d: %d\n",Case++,ans-);
  61. }
  62. return ;
  63. }

回文串---Palindrome的更多相关文章

  1. [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

  3. LeetCode 131. 分割回文串(Palindrome Partitioning)

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

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

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

  5. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  6. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

  9. hdu 1159 Palindrome(回文串) 动态规划

    题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...

随机推荐

  1. No Assistant Results

    由于修改一些文件名字等会导致这个不工作. "Organizer" / "Projects" / 选择你的项目.  "Delete" .

  2. [转]jQuery.Autocomplete实现自动完成功能(详解)

    本篇文章除了介绍jquery.autocomplete基本参数外,主要说明jquery.autocomplete的数据源的格式问题.     1.jquery.autocomplete参考地址 htt ...

  3. Windows开发环境搭建(安装 VS2010, VS2013, VS2015 Community, Windows Server 2008 R2)

    1. 安装VS2010 1.1 安装步骤 1. 注意安装的时候,选择自定义安装,将不需要的VB.net去掉. 2. 看一下C++下的x64选项是否选择了,如果没选,将其选上. 3. 一定要将 Micr ...

  4. 无法打开包括文件:'atlrx.h'的解决办法

    VS 2008中由于将ALT项目的部分代码剥离出去成为了独立的开源项目,需要用到ALT中正则表达式等功能就需要手动下载. 我不是第一个遇到这个问题的,所以已经有前人给出了解决方案. 可到http:// ...

  5. 解决HP打印机错误:Couldn't open fifo

    我的是因为选错了打印机协议,一开始选成了“互联网打印协议 - IPP”. 解决方案:删除原有打印机配置,重新选择协议为“HP Jetdirect-Socket”即可.

  6. Mac OSX 安装nvm(node.js版本管理器)

    我的系统 1.打开github官网https://github.com/,输入nvm搜索,选择creationix/nvm,打开 2.找到Install script,复制 curl -o- http ...

  7. Git 执行 「fork 出来的仓库」和「最新版本的原仓库」内容同步更新

    当我们在 GitHub 上 fork 出一个仓库后,如果原仓库更新了,此时怎样才能保证我们 fork 出来的仓库和原仓库内容一致呢?我们一般关注的是仓库的 master(主干分支)的内容,通过以下步骤 ...

  8. 微信、qq时间格式模板

    产品近来蛋疼,时间格式从做完到现在改了四遍了 ,最新的要求如下: * 2分钟内 无显示 * 2分钟-24小时 HH:mm * 昨天 昨天 HH:mm * 前天 前天 HH:mm * 今年 MM:DD ...

  9. 从逆向的角度去理解C++虚函数表

    很久没有写过文章了,自己一直是做C/C++开发的,我一直认为,作为一个C/C++程序员,如果能够好好学一下汇编和逆向分析,那么对于我们去理解C/C++将会有很大的帮助,因为程序中所有的奥秘都藏在汇编中 ...

  10. Eclipse启动分析

    最近研究了一下Eclipse的启动过程,查了点资料,也有点收获.下面是搜集到的一个比较好的说明,共享下... 在Eclipse启动过程中,Eclipse.exe负责启动,也就是把startup.jar ...