-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest Palindrome Subsequence.cpp
More file actions
61 lines (61 loc) · 1.54 KB
/
Copy pathLongest Palindrome Subsequence.cpp
File metadata and controls
61 lines (61 loc) · 1.54 KB
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
#include <iostream>
#define int long long int
using namespace std;
int LPS(string str,int i,int j,int **dp){
if (i>j){
dp[i][j] = 0;
return dp[i][j];
}
if (i==j){
dp[i][j] = 1;
return dp[i][j];
}
if (str[i-1] == str[j-1]){
dp[i][j] = 2 + LPS(str,i+1,j-1,dp);
return dp[i][j];
}
dp[i][j] = max(LPS(str,i+1,j,dp), LPS(str,i,j-1,dp));
return dp[i][j];
}
int LPS2(string str,int n){
int **dp = new int*[n+1];
for (int i = 0; i <=n; ++i) {
dp[i] = new int[n+1];
}
for (int i = 0; i <=n; ++i) {
for (int j = 0; j <=n; ++j) {
dp[i][j] = 0;
}
}
for (int i = n; i >0; --i) {
for (int j = i; j <=n; ++j) {
if (i==j){
dp[i][j] = 1;
} else if (str[i-1] == str[j-1]){
dp[i][j] = 2 + dp[i+1][j-1];
} else{
dp[i][j] = max(dp[i+1][j],dp[i][j-1]);
}
}
}
return dp[1][n];
}
int32_t main(){
cout<<"Enter A String :\n";
string str;
cin>>str;
int n = str.length();
cout<<"Using Top Down Approach :\n";
int **dp = new int*[n+1];
for (int i = 0; i <=n; ++i) {
dp[i] = new int[n+1];
}
for (int i = 0; i <=n; ++i) {
for (int j = 0; j <=n; ++j) {
dp[i][j] = -1;
}
}
cout<<"Maximum Length Palindrome Subsequence is : "<<LPS(str,1,n,dp)<<"\n";
cout<<"Using Bottom Up Approach :\n";
cout<<"Maximum Length Palindrome Subsequence is : "<<LPS2(str,n)<<"\n";
}