Longest Palindromic Subsequence 题解

题目来源:https://leetcode.com/problems/longest-palindromic-subsequence/description/


Description

Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.

Example 1:

Input:

"bbbab"

Output:

4

One possible longest palindromic subsequence is "bbbb".

Example 2:

Input:

"cbbd"

Output:

2

Solution

class Solution {
public:
int longestPalindromeSubseq(string str) {
int n = str.length(), high, low;
vector<vector<int>> dp(n, vector<int>(n, 0));
for (high = 0; high < n; high++) {
dp[high][high] = 1;
for (low = high - 1; low >= 0; low--) {
if (str[low] != str[high])
dp[high][low] = max(dp[high][low + 1], dp[high - 1][low]);
else
dp[high][low] = dp[high - 1][low + 1] + 2;
}
} return dp[n - 1][0];
}
};

解题描述

这道题是经典的求字符串中最长回文子序列的长度的问题。采用的办法是动态规划:

  • 对一个字符串,如果头尾的字符不同,该字符串的最长回文子序列的长度就是去掉头字符的的子串的最长回文子序列的长度和去掉尾字符的的子串的最长回文子序列的长度二者中较大值
  • 如果头尾的字符相同,该字符串的最长回文子序列的长度就是去掉头尾字符的的子串的最长回文子序列的长度+2

上面的解法中,按理论来说dp第一个下标可以是low,但是如果将highlow对调的话较为符合局部性原理,数据可以更多地在cache中读取,减少了访问内存的次数,因此速度较快(实际在LeetCode上的测试结果也是如此)。

拓展

如果问题改成求一个字符串的回文子序列数目(或者将一个字符串去掉某些字符以获得一个回文字符串的方案数)的话,同样可以使用动态规划的办法,状态转移上面也是针对头尾字符是否相同进行区分:

  • 如果头尾字符不同,字符串的回文子序列数目 = 去掉头字符后字符串的回文子序列数目 + 去掉尾字符后字符串的回文子序列数目 - 去掉头尾字符后字符串的回文子序列数目
  • 如果头尾字符相同,字符串的回文子序列数目 = 上一情况的结果 + 去掉头尾字符后字符串的回文子序列数目 + 1 = 去掉头字符后字符串的回文子序列数目 + 去掉尾字符后字符串的回文子序列数目 + 1

[Leetcode] Longest Palindromic Subsequence的更多相关文章

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

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

  2. LeetCode——Longest Palindromic Subsequence

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

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

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

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

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

  5. LN : leetcode 516 Longest Palindromic Subsequence

    lc 516 Longest Palindromic Subsequence 516 Longest Palindromic Subsequence Given a string s, find th ...

  6. [leetcode]516. Longest Palindromic Subsequence最大回文子序列

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

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

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

  8. [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence

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

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

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

随机推荐

  1. css 层加透明度后文字依然清晰

    background: rgba(, , , !important; /*实现FF背景透明,文字不透明*/ filter: Alpha(opacity=); background: #0a0a0a; ...

  2. hbase windows安装

    下载目前最新版本 http://mirrors.hust.edu.cn/apache/hbase/stable/ 最新版本 hbase-1.2.6 1. 解压到D:\software\hbase-1. ...

  3. 【uoj#192】[UR #14]最强跳蚤 Hash

    题目描述 给定一棵 $n$ 个点的树,边有边权.求简单路径上的边的乘积为完全平方数的点对 $(x,y)\ ,\ x\ne y$ 的数目. 题解 Hash 一个数是完全平方数,当且仅当每个质因子出现次数 ...

  4. Immediate Decodability HDU - 1305(模板trie)

    求这些01串是否有一个是另一个的前缀.. 就是求次数就好了嘛...emm... 网上竟然都用指针写.... #include<cstdio> #include<iostream> ...

  5. CF954F Runner's Problem(动态规划,矩阵快速幂)

    CF954F Runner's Problem(动态规划,矩阵快速幂) 题面 CodeForces 翻译: 有一个\(3\times M\)的田野 一开始你在\((1,2)\)位置 如果你在\((i, ...

  6. 洛谷 U14475 部落冲突 【比赛】 【树链剖分 + 线段树】

    题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...

  7. 51nod 1766 树上的最远点对(线段树)

    像树的直径一样,两个集合的最长路也是由两个集合内部的最长路的两个端点组成的,于是我们知道了两个集合的最长路,枚举一下两两端点算出答案就可以合并了,所以就可以用线段树维护一个区间里的最长路了. #inc ...

  8. C/C++中如何计算程序运行的时间

    一个程序的功能通常有很多种方法来实现,怎么样的程序才算得上最优呢?举个例子,如果实现同一个功能的两个程序,一个一点按钮就给出运行结果,而另一个则需要漫长的时间去等待,就像安装WINDOWS XP一样( ...

  9. Canny边缘检测算法原理及其VC实现详解(二)

    转自:http://blog.csdn.net/likezhaobin/article/details/6892629 3.  Canny算法的实现流程 由于本文主要目的在于学习和实现算法,而对于图像 ...

  10. 简化版SMO算法标注

    ''' 随机选择随机数,不等于J ''' def selectJrand(i,m): j=i #we want to select any J not equal to i while (j==i): ...