-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_LongestPalindromicSubsequence.cpp
87 lines (77 loc) · 2.1 KB
/
GFG_LongestPalindromicSubsequence.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
https://leetcode.com/problems/longest-palindromic-subsequence/
Longest Palindromic Subsequence
https://practice.geeksforgeeks.org/problems/longest-palindromic-subsequence-1612327878/1
*/
class Solution{
public:
int longestPalinSubseq(string A) {
int n = A.length();
vector<int> dp(n, 0);
for(int i=0; i<n; i++)
{
dp[i] = 1;
int maxsofar=0;
for(int j=i-1; j>=0; --j)
{
int prev = dp[j];
if(A[i]==A[j])
dp[j] = maxsofar + 2;
maxsofar = max(maxsofar, prev);
}
}
return *max_element(dp.begin(), dp.end());
}
int longestPalinSubseq_1(string A) {
int n = A.length();
vector<vector<int>> dp(n, vector<int>(n, 0));
for(int i=0; i<n; i++)
dp[i][i] = 1;
for(int k=2; k<=n; k++)
{
for(int i=0; i<n-k+1; ++i)
{
int j = i+k-1;
if(A[i]==A[j])
dp[i][j] = dp[i+1][j-1] + 2;
else
dp[i][j] = max(dp[i+1][j], dp[i][j-1]);
}
}
// cout<<" ";
// for(int i=0; i<n; i++)
// {
// cout<<A[i]<<" ";
// }
// cout<<endl;
// for(int i=0; i<n; i++)
// {
// cout<<A[i]<<" ";
// for(int j=0; j<n; j++)
// {
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
return dp[0][n-1];
}
int longestPalinSubseq_(string A) {
string B = A;
reverse(B.begin(), B.end());
int n = A.length();
vector<vector<int>> dp(n+1, vector<int>(n+1, 0));
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(A[i-1] == B[j-1])
dp[i][j] = 1+dp[i-1][j-1];
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
// cout<<dp[i][j]<<" ";
}
// cout<<endl;
}
return dp[n][n];
}
};