【Longest Palindromic Substring】cpp
题目:
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的更多相关文章
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- 【Longest Common Prefix】cpp
题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...
- 【Longest Consecutive Sequence】cpp
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- 【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 ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- 【LeetCode】5. Longest Palindromic Substring 最大回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【leetcode】5. Longest Palindromic Substring
题目描述: Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 【leetcode】Longest Palindromic Substring (middle) 经典
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- JavaScript高级 面向对象的程序设计 (二)《JavaScript高级程序设计(第三版)》
二.继承 OO是面向对象语言最为有魅力的概念.一般的OO语言都实现了两种继承,接口继承和实现继承.接口继承只继承方法签名,而实际继承继承了实际的方法. 而在JS中,函数没有签名,所以无法实现接口继承. ...
- ubuntu 12.04 64位设置兼容32位的实现
在ubuntu12.04上,要运行32的程序,需要安装32位的兼容库. 以前在10.04上成功安装过,方法是 sudo apt-get install ia32-libs 但是在12.04上遇到了困难 ...
- static local variable
Putting the keyword static in front of a local variable declaration creates a special type of variab ...
- Check Box Select/Deselect All on Grid
The below function is to be used on a grid with multiple check boxes. Place the code behind a FieldC ...
- Winform登录、控制软件只运行一次、回车登录
Winform登录对很多程序猿来说都有些困惑,登录进入主窗体后要销毁登录窗体,而不是隐藏哦,怎么实现呢? 先贴一段Program.cs的代码 static void Main() { Mutex mu ...
- php 显示内存 释放内存
<?php //这只是个例子,下面的数字取决于你的系统 echo memory_get_usage() . "\n"; // 36640 $a = str_repeat(&q ...
- C# 判断是否为闰年的条件各是
//try 没增加异常数据处理 Console.WriteLine("根据输入的信息计算当年某个月份的天数,以及当年是否是闰年或平年,\n并判断2月份特殊月份的天数."); Con ...
- Java 第三天 Gradle和其它
Gradle 是以 Groovy 语言为基础,面向Java应用为主.基于DSL(领域特定语言)语法的自动化构建工具. 下载地址 http://www.gradle.org/downloads 环境变量 ...
- Flask Web Development —— Web表单(上)
Flask-WTF扩展使得处理web表单能获得更愉快的体验.该扩展是一个封装了与框架无关的WTForms包的Flask集成. Flask-WTF和它的依赖集可以通过pip来安装: (venv) $ p ...
- mysql外键设置(待测试)
外键的定义语法:[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...) REFERENCES tbl_name (index_col ...