【翻译】Longest Palindromic Substring 最长回文子串
原文地址:
http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html
转载请注明出处:http://www.cnblogs.com/zhxshseu/p/4947609.html
问题描述:Given a string S, find the longest palindromic substring in S.
基本条件是:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
string longestPalindromeDP(string s) {
int n = s.length();
int longestBegin = 0;
int maxLen = 1;
bool table[1000][1000] = {false};
for (int i = 0; i < n; i++) {
table[i][i] = true;
}
for (int i = 0; i < n-1; i++) {
if (s[i] == s[i+1]) {
table[i][i+1] = true;
longestBegin = i;
maxLen = 2;
}
}
for (int len = 3; len <= n; len++) {//对长度为3,4,5……的子串进行遍历
for (int i = 0; i < n-len+1; i++) {//以len为窗口,在s上进行平移,判断是否符合递推条件
int j = i+len-1;
if (s[i] == s[j] && table[i+1][j-1]) {
table[i][j] = true;
longestBegin = i;
maxLen = len;
}
}
}
return s.substr(longestBegin, maxLen);
}
|
举例:cabccbad
第一次循环以后,table值如下

第二次循环以后,table值如下:








|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
string expandAroundCenter(string s, int c1, int c2) {
int l = c1, r = c2;
int n = s.length();
while (l >= 0 && r <= n-1 && s[l] == s[r]) {
l--;
r++;
}
return s.substr(l+1, r-l-1);
}
string longestPalindromeSimple(string s) {
int n = s.length();
if (n == 0) return "";
string longest = s.substr(0, 1); // c single char itself is a palindrome
for (int i = 0; i < n-1; i++) {//遍历整个字符串
string p1 = expandAroundCenter(s, i, i);//以该位置字符为中心展开,奇数长
if (p1.length() > longest.length())
longest = p1;
string p2 = expandAroundCenter(s, i, i+1);//以该字符后面的空隙展开,偶数长
if (p2.length() > longest.length())
longest = p2;
}
return longest;
}
|
举例:cabccbad
初始时,i=0 (奇 代表奇数长子串,偶 代表偶数长子串)






【翻译】Longest Palindromic Substring 最长回文子串的更多相关文章
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- lintcode :Longest Palindromic Substring 最长回文子串
题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...
- [leetcode]5. Longest Palindromic Substring最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- [LeetCode] 5. Longest Palindromic Substring 最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 【LeetCode】5. Longest Palindromic Substring 最长回文子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...
- 1. Longest Palindromic Substring ( 最长回文子串 )
要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...
- LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题意很简单,就是求一个字符串得最长子串,这里的子串指连续的. 本文给 ...
随机推荐
- 学到的较复杂的 mysql 语名
需求是这样的:需要找出没有发任务的店铺是哪些.好让客服去联系他们,询问情况. 每个商家可以有N个店铺,每个店铺可以放N个任务,一个任务会生成N个任务订单,会员接到任务去完成 SELECT *FROM ...
- 开机启动遇到grub rescue,无法启动系统解决方法
先使用ls命令,找到Ubuntu的安装在哪个分区: grub rescue>ls 会罗列出磁盘信息 (hd0) (hd0,msdos9) (hd0,msdos8) (hd0,msdos7).. ...
- ZOJ 1003 Crashing Balloon
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...
- 理解Load Average做好压力测试
http://www.blogjava.net/cenwenchu/archive/2008/06/30/211712.html CPU时间片 为了提高程序执行效率,大家在很多应用中都采用了多线程模式 ...
- C#的GridView控件复习
一,在新建一个空网站 二,在这网站新建一个default.aspx页面 三,添加一个GridView控件 四,新建一个数据库,这个数据库包含你要显示的表数据,这个表须包含主键(表的主键影响增删改的功能 ...
- 《JavaScript网页特效经典300例-进阶篇》
<Javascript网页经典特性300例> 进阶篇 第11章:导航菜单特效 二级导航菜单三级导航菜单动态加载导航菜单三级联动导航菜单树形导航菜单当网页超过一屏时导航菜单始终置顶 第12章 ...
- 使用php创建WebSocket服务
执行方法:首先先修改server.php与index.html的ip通过命令行执行 [php路径]php.exe "[文件路径]server.php"然后通过浏览器打开index. ...
- 《Intel汇编第5版》 Intel CPU小端序
一.MASM汇编器中的数据类型 二.Intel汇编中的立即数类型 三.定义有符号和无符号整数 四.小端序 内存中数据按照字节存储,一个4个字节无符号整数,其高位存储在低地址上,低位存储在高地址上. 比 ...
- poj 1411 Calling Extraterrestrial Intelligence Again
题意:给你数m,a,b,假设有数p,q,满足p*q<=m同时a/b<=p/q<=1,求当p*q最大的p和q的值 方法:暴力枚举 -_-|| and 优化范围 我们可以注意到在某一个m ...
- cmd alias 自定义命令
简短步骤:1.关闭所有在运行的CMD窗口2.创建文件C:\cmd-alias.bat,包含以下内容:[python] view plain copydoskey sayhello=echo Hello ...