Longest Palindromic Subsequence

方法:与substring的不同是这里不需要子串连续

class Solution {
public:
    int longestPalindromeSubseq(string s) {
        vector<vector<int>> f(s.size(), vector<int>(s.size(), 0));

        for(int i=s.size()-1; i>=0; --i)
        {
            f[i][i] = 1;
            for(int j=i+1; j<s.size(); ++j)
            {
                if(s[i] == s[j])
                    f[i][j] = f[i+1][j-1] + 2;
                else
                    f[i][j] = max(f[i+1][j], f[i][j-1]);
            }
        }

        return f[0][s.size()-1];
    }
};

或者

class Solution {
public:
    int longestPalindromeSubseq(string s) {
        vector<vector<int>> f(s.size(), vector<int>(s.size(), 0));

        for(int i=0; i<s.size(); ++i)
        {
            f[i][i] = 1;
            for(int j=i-1; j>=0; --j)
            {
                if(s[i] == s[j])
                    f[j][i] = f[j+1][i-1] + 2;
                else
                    f[j][i] = max(f[j+1][i], f[j][i-1]);
            }
        }

        return f[0][s.size()-1];
    }
};
时间: 2024-10-11 10:48:11

Longest Palindromic Subsequence的相关文章

LeetCode 516. Longest Palindromic Subsequence

516. Longest Palindromic Subsequence Add to List Description Submission Solutions Total Accepted: 2159 Total Submissions: 5216 Difficulty: Medium Contributors: Stomach_ache Given a string s, find the longest palindromic subsequence's length in s. You

Longest Palindromic Substring &amp; Longest Palindromic Subsequence

5. Longest Palindromic Substring 题目链接:https://leetcode.com/problems/longest-palindromic-substring/#/description 题目大意:给定一个字符串s,返回该字符串的最长回文子串.s的最大长度不超过1000. 思路:对于每个子串的中心(可以是一个字符,或者是两个字符的间隙,比如串abc,中心可以是a,b,c,或者是ab的间隙,bc的间隙)往两边同时进行扫描,直到不是回文串为止.假设字符串的长度为n

516. Longest Palindromic Subsequence

本周课堂上学习的是动态规划,因此在LeetCode上找到相应的题进行练习. 题目: Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000. (给定一个字符串S,找到它的最长回文子序列长度) 示例: Example 1:Input  :"bbbab" Output:4 (One possibl

LC 516. Longest Palindromic Subsequence

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:

516 Longest Palindromic Subsequence 最长回文子序列

给定一个字符串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<in

[译]最长回文子串(Longest Palindromic Substring) Part II

[译+改]最长回文子串(Longest Palindromic Substring) Part II 问题:给定字符串S,求S中的最长回文子串. 在上一篇,我们给出了4种算法,其中包括一个O(N2)时间O(1)空间的算法(中心检测法),已经很不错了.本篇将讨论一个O(N)时间O(N)空间的算法,即著名的Manacher算法,并详细说明其时间复杂度为何是O(N). 提示 +BIT祝威+悄悄在此留下版了个权的信息说: 先想想有什么办法能改进中心检测法. 考虑一下最坏的情况.★ 最坏的情况就是各个回文

LPS UVA 11404 Palindromic Subsequence

题目传送门 1 /* 2 LPS(Longest Palidromic Subsequence):最长回文子序列,和回文串不同,子序列是可以不连续的. 3 转化为LCS问题,将字符串逆序,然后和本串求LCS就是LPS的长度(为啥不就是LPS?),但是前一半是LPS的一半,可以补全 4 推荐->还有一种写法是用了LCS的思想,dp[i][j]表示i到j的最长回文串长度,状态转移方程: 5 1. dp[j][j+i-1] = dp[j+1][j+i-2] + 2; (str[j] == str[j+

LeetCode 1143. Longest Common Subsequence

原题链接在这里:https://leetcode.com/problems/longest-common-subsequence/ 题目: Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some cha

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)