【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 ...
随机推荐
- s3c6410_中断
参考: 1)<USER'S MANUAL-S3C6410X>Chapter 10 GPIO, Chapter 12 VECTORED INTERRUPT CONTROLLER 2)< ...
- eclipse 中maven项目右键没有maven菜单问题
修改项目.project文件,确保有maven2Builder和maven2Nature2个标签: <?xml version="1.0" encoding="UT ...
- Thinking about think-time functions
You will find yourself very familier to this topic. Ok, let me ask you one question: Let me know th ...
- dataGridView 如何默认选中第一行
datagridview默认选中第一行方法: this.dataGridView1.Rows[0].Selected = true; datagridview 去除 默认选中第一行方法:在绑定data ...
- Ajax与Json的一些总结
Ajax与Json AJAX=异步javaScript 和XML AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新. 这意味着可以在不 ...
- Ajax的利弊
ajax的优点 1.最大的一点是页面无刷新,在页面内与服务器通信,给用户的体验非常好. 2.使用异步方式与服务器通信,不需要打断用户的操作,具有更加迅速的响应能力. 3.可以把以前一些服务器负担的工 ...
- Redis监控方案
Redis 监控最直接的方法当然就是使用系统提供的 info 命令来做了,你只需要执行下面一条命令,就能获得 Redis 系统的状态报告. redis-cli info 内存使用 如果 Redis 使 ...
- CentOS6.4安装VNC
http://jingyan.baidu.com/article/ca2d939dd1dabbeb6c31ce24.html 一.安装 VNC 默认情况下,CentOS 6.4 是没有安装的. 检查是 ...
- SqlBulkCopy 插入100W条数据时 属性BatchSize的作用
(1)100W条insert语句在一个连接内一句一句加 花了01:17:19.0542805 (2) SqlBulkCopy 插入100W条数据 设置BatchSize=500 耗时:00:03:29 ...
- C#条件编译,发布多平台和多种选择性的项目
http://www.cnblogs.com/chengulv/p/4579528.html 界面操作参考 这样正对不同环境就可以编译出不同的exe或者dll,做到一个项目的灵活多变.条件编译还可以满 ...