给定一个字符串s,找到其中最长的回文子序列。可以假设s的最大长度为1000。

详见:https://leetcode.com/problems/longest-palindromic-subsequence/description/

C++:

class Solution {
public:
int longestPalindromeSubseq(string s)
{
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n));
for (int i = n - 1; i >= 0; --i)
{
dp[i][i] = 1;
for (int j = i + 1; j < n; ++j)
{
if (s[i] == s[j])
{
dp[i][j] = dp[i + 1][j - 1] + 2;
}
else
{
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
}
}
}
return dp[0][n - 1];
}
};

参考:http://www.cnblogs.com/grandyang/p/6493182.html

516 Longest Palindromic Subsequence 最长回文子序列的更多相关文章

  1. [LeetCode] 516. Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  2. 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...

  3. [LeetCode] Longest Palindromic Subsequence 最长回文子序列

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  4. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  5. 516. Longest Palindromic Subsequence最长的不连续回文串的长度

    [抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...

  6. [LeetCode] Longest Palindromic Substring 最长回文串

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

  7. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

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

  8. 1. Longest Palindromic Substring ( 最长回文子串 )

    要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...

  9. LeetCode:Longest Palindromic Substring 最长回文子串

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

随机推荐

  1. iptables apache2

    Apache2 iptables 安装指令:sudo apt-get install apache2 2.产生的启动和停止文件是:/etc/init.d/apache2 3.启动:sudo apach ...

  2. gitlab merge过程

    基本步骤如下: 以我的分支为例 1.创建本地分支,命令 git checkout -b liuping_develop2.创建好分支后提交到远程 ,命令 git push origin liuping ...

  3. poj 3585 Accumulation Degree(二次扫描和换根法)

    Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...

  4. mysql 数据库导入错误:40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET

    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RES ...

  5. redis05----Redis 中的事务

    Redis支持简单的事务 Redis与 mysql事务的对比 Mysql Redis 开启 start transaction multi 语句 普通sql 普通命令 失败 rollback 回滚 d ...

  6. 序列化FastReport,重要提示少走弯路 good

    原本在开发一个报表插件,因为需要远程传输,因此需要序列化报表,序列化FastReport有两种方式, 1.仅序列化数据,由客户端接受到数据,并呈现报表,这种方式需要在客户端存储报表格式文件xxx.Fr ...

  7. e.target与e.currentTarget的区别

    在DOM事件对象中有两个属性总是时不时的困扰我,就是target和currentTarget,有时候很迷惑分不清两者的区别,因此有必要把这两个属性好好梳理一下,加深理解,以便日后的查询. MDN中对t ...

  8. iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用

    目的 本文主要是分享iOS多线程的相关内容,为了更系统的讲解,将分为以下7个方面来展开描述. 多线程的基本概念 线程的状态与生命周期 多线程的四种解决方案:pthread,NSThread,GCD,N ...

  9. 一步一步学Silverlight 2系列(17):数据与通信之ADO.NET Data Services

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  10. I.MX6 u-boot 2009 lvds hdmi lcd 补丁

    /************************************************************************* * I.MX6 u-boot 2009 lvds ...