|
| 1 | +// Source : https://leetcode.com/problems/longest-palindromic-subsequence/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-03-27 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * Given a string s, find the longest palindromic subsequence's length in s. |
| 8 | + * |
| 9 | + * A subsequence is a sequence that can be derived from another sequence by deleting some or no |
| 10 | + * elements without changing the order of the remaining elements. |
| 11 | + * |
| 12 | + * Example 1: |
| 13 | + * |
| 14 | + * Input: s = "bbbab" |
| 15 | + * Output: 4 |
| 16 | + * Explanation: One possible longest palindromic subsequence is "bbbb". |
| 17 | + * |
| 18 | + * Example 2: |
| 19 | + * |
| 20 | + * Input: s = "cbbd" |
| 21 | + * Output: 2 |
| 22 | + * Explanation: One possible longest palindromic subsequence is "bb". |
| 23 | + * |
| 24 | + * Constraints: |
| 25 | + * |
| 26 | + * 1 <= s.length <= 1000 |
| 27 | + * s consists only of lowercase English letters. |
| 28 | + ******************************************************************************************************/ |
| 29 | + |
| 30 | +/* |
| 31 | +
|
| 32 | + supposed s = "abbcba" |
| 33 | + |
| 34 | + we can have a matrix, |
| 35 | + |
| 36 | + - dp[start, end] is the longest from s[start] to s[end] |
| 37 | + |
| 38 | + - if (start == end) dp[statr, end] = 1, it means every char can be palindromic |
| 39 | + |
| 40 | + a b b c b a |
| 41 | + a 1 0 0 0 0 0 |
| 42 | + b 0 1 0 0 0 0 |
| 43 | + b 0 0 1 0 0 0 |
| 44 | + c 0 0 0 1 0 0 |
| 45 | + b 0 0 0 0 1 0 |
| 46 | + a 0 0 0 0 0 1 |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + calculating from the bottom to up. (Note: only care about the top-right trangle) |
| 51 | + |
| 52 | + a b b c b a |
| 53 | + a 1 1 2 2 3 [5] <-- a == a , so "abbcba" comes from "bbcb" + 2 |
| 54 | + b 0 1 [2] 2 3 3 <-- b == b , so "bb" comes from "" + 2 |
| 55 | + b 0 0 1 1 [3] 3 <-- b == b , so "bcb" comes from "c" + 2 |
| 56 | + c 0 0 0 1 1 [1] <-- c != a , so "cba" comes from max("cb", "a") |
| 57 | + b 0 0 0 0 1 [1] <-- b != a , so "ba" comes from max ("b", "a") |
| 58 | + a 0 0 0 0 0 1 |
| 59 | + |
| 60 | + So, we can have the following formular: |
| 61 | + |
| 62 | + s[start] != s[end] ==> dp[start, end] = max (dp[start+1, end], dp[start, end-1]); |
| 63 | + s[start] == s[end] ==> dp[start, end] = dp[start+1, end-1] + 2; |
| 64 | +
|
| 65 | +*/ |
| 66 | + |
| 67 | + |
| 68 | +class Solution { |
| 69 | +public: |
| 70 | + int longestPalindromeSubseq(string s) { |
| 71 | + int n = s.size(); |
| 72 | + vector<vector<int>> dp(n, vector<int>(n, 0)); |
| 73 | + |
| 74 | + for (int start = n-1; start>=0; start--) { |
| 75 | + for (int end = start ; end < n ; end++) { |
| 76 | + if (start == end) { |
| 77 | + dp[start][end] = 1; |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (s[start] == s[end]) { |
| 81 | + dp[start][end] = dp[start+1][end-1] + 2; |
| 82 | + }else{ |
| 83 | + dp[start][end] = max (dp[start+1][end], dp[start][end-1]); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + } |
| 88 | + return dp[0][n-1]; |
| 89 | + } |
| 90 | +}; |
0 commit comments