回文串---Palindrome
Description
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
Output
Sample Input
- abcbabcbabcba
- abacacbaaaab
- END
Sample Output
- Case 1: 13
- Case 2: 6
- 题意:给一个字符串求这个串的最长回文子串的长度;
- 思路:用以下回文串的模板可以在线性时间内完成求最长的回文串的长度;
- #include<iostream>
- #include<algorithm>
- #include<cstdio>
- #include<cstring>
- using namespace std;
- const int N=;
- int n,p[N];
- char s[N],str[N];
- void kp()
- {
- int i;
- int mx=;
- int id;
- ///for(i=n;str[i]!=0;i++)
- ///str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba;
- for(i=;i<n;i++)
- {
- if(mx>i)
- p[i]=min(p[*id-i],p[id]+id-i);
- else
- p[i]=;
- 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[]='$';
- str[]='#';
- for(int i=;i<n;i++)
- {
- str[i*+]=s[i];
- str[i*+]='#';
- }
- n=n*+;
- s[n]=;
- }
- int main()
- {
- int Case=;
- while(scanf("%s",s)!=EOF)
- {
- if(s[]=='E'&&s[]=='N'&&s[]=='D')
- break;
- n=strlen(s);
- init();
- kp();
- int ans=;
- for(int i=;i<n;i++)
- if(p[i]>ans)
- ans=p[i];
- printf("Case %d: %d\n",Case++,ans-);
- }
- return ;
- }
回文串---Palindrome的更多相关文章
- [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 分割回文串 · Palindrome Partitioning
[抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- hdu 1159 Palindrome(回文串) 动态规划
题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...
随机推荐
- No Assistant Results
由于修改一些文件名字等会导致这个不工作. "Organizer" / "Projects" / 选择你的项目. "Delete" .
- [转]jQuery.Autocomplete实现自动完成功能(详解)
本篇文章除了介绍jquery.autocomplete基本参数外,主要说明jquery.autocomplete的数据源的格式问题. 1.jquery.autocomplete参考地址 htt ...
- Windows开发环境搭建(安装 VS2010, VS2013, VS2015 Community, Windows Server 2008 R2)
1. 安装VS2010 1.1 安装步骤 1. 注意安装的时候,选择自定义安装,将不需要的VB.net去掉. 2. 看一下C++下的x64选项是否选择了,如果没选,将其选上. 3. 一定要将 Micr ...
- 无法打开包括文件:'atlrx.h'的解决办法
VS 2008中由于将ALT项目的部分代码剥离出去成为了独立的开源项目,需要用到ALT中正则表达式等功能就需要手动下载. 我不是第一个遇到这个问题的,所以已经有前人给出了解决方案. 可到http:// ...
- 解决HP打印机错误:Couldn't open fifo
我的是因为选错了打印机协议,一开始选成了“互联网打印协议 - IPP”. 解决方案:删除原有打印机配置,重新选择协议为“HP Jetdirect-Socket”即可.
- Mac OSX 安装nvm(node.js版本管理器)
我的系统 1.打开github官网https://github.com/,输入nvm搜索,选择creationix/nvm,打开 2.找到Install script,复制 curl -o- http ...
- Git 执行 「fork 出来的仓库」和「最新版本的原仓库」内容同步更新
当我们在 GitHub 上 fork 出一个仓库后,如果原仓库更新了,此时怎样才能保证我们 fork 出来的仓库和原仓库内容一致呢?我们一般关注的是仓库的 master(主干分支)的内容,通过以下步骤 ...
- 微信、qq时间格式模板
产品近来蛋疼,时间格式从做完到现在改了四遍了 ,最新的要求如下: * 2分钟内 无显示 * 2分钟-24小时 HH:mm * 昨天 昨天 HH:mm * 前天 前天 HH:mm * 今年 MM:DD ...
- 从逆向的角度去理解C++虚函数表
很久没有写过文章了,自己一直是做C/C++开发的,我一直认为,作为一个C/C++程序员,如果能够好好学一下汇编和逆向分析,那么对于我们去理解C/C++将会有很大的帮助,因为程序中所有的奥秘都藏在汇编中 ...
- Eclipse启动分析
最近研究了一下Eclipse的启动过程,查了点资料,也有点收获.下面是搜集到的一个比较好的说明,共享下... 在Eclipse启动过程中,Eclipse.exe负责启动,也就是把startup.jar ...