-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
37 lines (35 loc) · 779 Bytes
/
main.cpp
File metadata and controls
37 lines (35 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
// dp
class Solution {
public:
int longestPalindromeSubseq(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n,0));
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];
}
};
int main()
{
Solution s;
cout << s.longestPalindromeSubseq("bbbab") << endl;
return 0;
}