题目:

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

代码:

class Solution {
public:
string longestPalindrome(string s) {
const size_t len = s.length();
// initialize dp matrix
bool dp[len][len];
std::fill_n(&dp[][], len*len, false);
dp[][] = true;
for ( size_t i = ; i < len; ++i )
{
dp[i][i] = true;
dp[i][i-] = true;
}
// dp process
size_t left = ;
size_t longest_palindrome = ;
for ( size_t k = ; k <= len; ++k )
{
for ( size_t i = ; i <= len-k; ++i )
{
if ( dp[i+][i+k-] && s[i]==s[i+k-] )
{
dp[i][i+k-] = true;
if ( longest_palindrome < k )
{
left = i;
longest_palindrome = k;
}
}
}
}
return s.substr(left,longest_palindrome);
}
};

tips:

采用动态规划思路,时间复杂度O(n²)。代码并不是最优的,但是相对简洁的(不用考虑奇数偶数的情况)。

判断一个子串是否是回文可以用其“掐头去尾”后的子子串是来判断:

a. 子子串是回文

b. 头等于尾

同时满足a,b则一定是回文;否则,一定不是回文。

这里设定一个dp[][]数组:dp[i][j]=true表示i到j这个子串是回文,dp[i][j]=false表示i到j这个子串不是回文。

对dp数组初始化时候需要注意两点:

(1)

显然dp[i][i]表示单个元素,都是回文,初始化为true。

(2)

dp[i][i-1]这种情况按理说是不存在的(因为左边的index不能大于右边的index),但是当k=2的时候,判断相邻两个字符是否构成回文的时候

有“dp[i+1][i+k-2]”这个情况,显然dp[i+1][i],此时这个判断其实是不起作用的,只用判断相邻两个元素相等即可;但是为了代码的简洁(都在一个循环中写下),强制令dp[1][0]、dp[2][1]、...、dp[len-1][len-2]都为true。

这里第一层循环k代表可能回文的长度(从2起),第二层循环i代表回文开始的位置。这里有一点要注意,就是k是可以取到len这个值的(即整个字符串就是一个大回文);并且,i是可以取到len-k的(因为最后一个字符的下标是len-1到len-k长度正好是k)。这两个边界细节要注意。

另,还有大Manacher算法,可以做到O(n)时间复杂度。以后再研究一下。

================================================

第二次过这道题,记得还用动归;但是具体指针下标迭代还需要考虑清楚。

class Solution {
public:
string longestPalindrome(string s) {
const int len = s.size();
bool dp[len][len];
std::fill_n(&dp[][], len*len, false);
for ( int i=; i<len; ++i ) dp[i][i]=true;
int l = ;
int r = ;
for ( int i=; i<len; ++i )
{
for ( int j=; j<i; ++j )
{
if ( i-j< )
{
dp[j][i] = s[i]==s[j];
if ( dp[j][i] && i-j>r-l )
{
l = j;
r = i;
}
}
else
{
dp[j][i] = dp[j+][i-] && s[i]==s[j];
if ( dp[j][i] && i-j>r-l )
{
l = j;
r = i;
}
}
}
}
return s.substr(l,r-l+);
}
};

【Longest Palindromic Substring】cpp的更多相关文章

  1. 【Longest Valid Parentheses】cpp

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  2. 【Longest Common Prefix】cpp

    题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...

  3. 【Longest Consecutive Sequence】cpp

    题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...

  4. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  5. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  6. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  7. 【LeetCode】5. Longest Palindromic Substring 最大回文子串

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  8. 【leetcode】5. Longest Palindromic Substring

    题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  9. 【leetcode】Longest Palindromic Substring (middle) 经典

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

随机推荐

  1. 前端培训教程 jquery半透明设置

    function setOpacity(eles){ $(eles).each(function(){ if($.browser.msie){ $(this).clone().empty().inse ...

  2. linux BASH shell设置字体与背景颜色

    linux BASH shell下设置字体及背景颜色的方法. BASH shell下设置字体及背景颜色  echo -e "\e[31mtest\e[41m"  \e[30m 将字 ...

  3. 魅族MX3问题集锦

    我第一台智能机已经服役2年半了,已经满足不了现在日益庞大的APP,所以打算让他光荣退役.我觉得IPhone仍然是目前做的最好的手机,但是对于我来说好像没什么必要,尤其那土豪般的价格.而且我平时看视频居 ...

  4. 重回程序员之路。重写博客。我的ecshop小京东二开问题汇总与解决方案。

    问题1:混合支付(余额支付+在线支付)不跟更新订单状态问题. 解决方案:http://bbs.ecshop.com/viewthread.php?tid=156761&highlight= i ...

  5. nandflash操作详解

    1.nandflash就是嵌入式系统的硬盘 2.分类(1)MLC:存储单元格存储两位,慢,偏移,寿命短,容量大(2)SLC:存储一位.快,寿命长,容量小,昂贵 3访问:(1)独立编址,有专用的控制器, ...

  6. hashCode()和toString()

    hashCode函数和toString函数也在Object类中,同样,所有的类都继承了这2个函数. hashCode函数用于生成哈希码,没有参数,返回值为整型 把u的值作为键存入map中,使用get方 ...

  7. ASP.NET实现弹出框真分页将复选框选择的数据存到数据库中(四)

    这是第四步点击保存将信息存入数据库中. 这个就简单了利用ajax将JSON字符串传到后台然后这里有个知识点就是将DataTable直接存入数据库中.代码如下: 一.界面获取数据JS代码: //保存订单 ...

  8. Percona-Xtrabackup 2.3.3 慢查询不再堵塞备份(一)

    在Percona-Xtrabackup 2.3.3[root@b28-19-17 bak]# rpm -q percona-xtrabackuppercona-xtrabackup-2.3.3-1.e ...

  9. Java并发编程实战---第六章:任务执行

    废话开篇 今天开始学习Java并发编程实战,很多大牛都推荐,所以为了能在并发编程的道路上留下点书本上的知识,所以也就有了这篇博文.今天主要学习的是任务执行章节,主要讲了任务执行定义.Executor. ...

  10. Android--用DownLoadManager下载完成后启动安装

    当我们用系统的服务DownLoadManager下载完成后,系统会发送一个广播,我们只需要注册一个广播,然后在广播里面写如一些相应的操作. 1.注册广播 completeReceiver = new ...