题目:

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. IOS懒人笔记应用源码

    这个源码是懒人笔记应用源码,也是一个已经上线的apple应用商店的应用,懒人笔记iOS客户端源码,支持语音识别,即将语音转化成文本文字,所用语音识别类库为讯飞语音类库. 懒人笔记是一款为懒人设计的笔记 ...

  2. u-boot board_uart_init流程

    /** ****************************************************************************** * @author    Maox ...

  3. Arch 安装步骤

    1.CFDISK 命令分区 一个主分区和一个逻辑分区 2.partprobe /dev/sdx 刷新分区表 3.mkfs 4. 5. 6.>dhcpcd 7. 8. 9. 10. 11. 12. ...

  4. Android四大组件一----Activity

    最新面试需要复习一下Android基础. {所谓Activity} 通俗点:app上看到的窗口基本都是Activity Android 程序一般是由多个Activity组成,用户看到的能够交互的窗口通 ...

  5. DevExpress控件开发常用要点(项目总结版)

    使用DevExpress控件来做项目开发已经有很长一段时间了,在摸索开发到客户苛刻要求的过程中,其中碰到过很多问题需要解决的,随着一个个问题的解决,也留下很多对DevExpress控件的使用经验及教训 ...

  6. hdu 5272 Dylans loves numbers

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5272 Dylans loves numbers Description Who is Dylans?Y ...

  7. hdu 1622 Trees on the level

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1622 小白书上的题... #include<algorithm> #include< ...

  8. 代码文档生成工具-Doxygen生成CHM和RTF图文教程

    Doxygen是一种开源跨平台的,以类似JavaDoc风格描述的文档系统,可以从一套归档源文件开始,生成chm格式的文档.本文主要讲解如何在winddows下安装doxygen.     1.下载do ...

  9. ios中怎么获得当前版本号

    NSString *version = [NSBundle mainBundle].infoDictionary[(__bridge NSString *)kCFBundleVersionKey];

  10. SQL Server 2008 R2 主从数据库同步

    一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2    DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...