博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
回文串---Palindrome
阅读量:5231 次
发布时间:2019-06-14

本文共 2264 字,大约阅读时间需要 7 分钟。

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

abcbabcbabcbaabacacbaaaabEND

Sample Output

Case 1: 13Case 2: 6 题意:给一个字符串求这个串的最长回文子串的长度; 思路:用以下回文串的模板可以在线性时间内完成求最长的回文串的长度;
#include
#include
#include
#include
using namespace std;const int N=2000010;int n,p[N];char s[N],str[N];void kp(){ int i; int mx=0; int id; ///for(i=n;str[i]!=0;i++) ///str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba; for(i=1;i
i) p[i]=min(p[2*id-i],p[id]+id-i); else p[i]=1; for( ;str[i+p[i]]==str[i-p[i]];p[i]++); if(p[i]+i>mx) { mx=p[i]+i; id=i; } }}void init(){ str[0]='$'; str[1]='#'; for(int i=0;i
ans) ans=p[i]; printf("Case %d: %d\n",Case++,ans-1); } return 0;}

 

转载于:https://www.cnblogs.com/chen9510/p/5400675.html

你可能感兴趣的文章
Python 第四十五章 MySQL 内容回顾
查看>>
实验2-2
查看>>
MongoDB遇到的疑似数据丢失的问题。不要用InsertMany!
查看>>
android smack MultiUserChat.getHostedRooms( NullPointerException)
查看>>
监控工具之---Prometheus 安装详解(三)
查看>>
不错的MVC文章
查看>>
IOS Google语音识别更新啦!!!
查看>>
[置顶] Linux终端中使用上一命令减少键盘输入
查看>>
BootScrap
查看>>
路冉的JavaScript学习笔记-2015年1月23日
查看>>
Mysql出现(10061)错误提示的暴力解决办法
查看>>
2018-2019-2 网络对抗技术 20165202 Exp3 免杀原理与实践
查看>>
NPM慢怎么办 - nrm切换资源镜像
查看>>
Swift - 异步加载各网站的favicon图标,并在单元格中显示
查看>>
【Python学习笔记】1.基础知识
查看>>
梦断代码阅读笔记02
查看>>
selenium学习中遇到的问题
查看>>
大数据学习之一——了解简单概念
查看>>
Linux升级内核教程(CentOS7)
查看>>
Lintcode: Partition Array
查看>>