516 Longest Palindromic Subsequence 最长回文子序列

Posted lina2014

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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<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 最长回文子序列的主要内容,如果未能解决你的问题,请参考以下文章

516. Longest Palindromic Subsequence

LeetCode 516. Longest Palindromic Subsequence

516. Longest Palindromic Subsequence

[leetcode-516-Longest Palindromic Subsequence]

LC 516. Longest Palindromic Subsequence

LeetCode-Longest Palindromic Subsequence