Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1.  int longestPalindromeSubseq(const string& s) {
  2.     string t(s);
  3.     reverse(t.begin(), t.end());
  4.     const int kN = s.size();
  5.     vector<vector<int>> longest_palindr_dp(kN + 1, vector<int>(kN + 1));
  6.    
  7.     for (int i = 1; i <= kN; ++i) {
  8.       for (int j = 1; j <= kN; ++j) {
  9.         int max_sofar = 0;
  10.         if (s[i - 1] == t[j - 1])
  11.           max_sofar = max(max_sofar, longest_palindr_dp[i-1][j-1] + 1);
  12.         else
  13.           max_sofar = max({max_sofar, longest_palindr_dp[i][j-1], longest_palindr_dp[i-1][j]});
  14.        
  15.         longest_palindr_dp[i][j] = max_sofar;
  16.       }
  17.     }
  18.      
  19.     return longest_palindr_dp.back().back();
  20.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement