-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestPalindromicSubsequence.cpp
More file actions
76 lines (71 loc) · 1.81 KB
/
longestPalindromicSubsequence.cpp
File metadata and controls
76 lines (71 loc) · 1.81 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <cstring>
#include <climits>
using namespace std;
int dp[105][105];
// DP
int longestPanidromeSunsequenceBUDP(string s){
int dp[100][100];
for(int i = 0;i<s.length();i++){
dp[i][i] = 1;
}
for(int len = 2;len<=s.length();len++){
for(int i=0;i<=s.length()-len;i++){
int j = i+len-1;
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]);
}
}
}
// for printing the dp array
// for(int i=0;i<s.size();i++){
// for(int j=0;j<s.size();j++){
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
return dp[0][s.size()-1]; // hamara answer (0,n-1) par padaa hoga
}
int longestPanidromeSunsequenceTDDP(string s,int start,int end){
if(start == end) return 1;
if(start > end) return 0;
int ans = 0;
if(dp[start][end] != 0){
return dp[start][end];
}
if(s[start] == s[end]){
ans = longestPanidromeSunsequenceTDDP(s,start + 1,end - 1) + 2;
}
else{
ans = max(longestPanidromeSunsequenceTDDP(s,start+1,end),longestPanidromeSunsequenceTDDP(s,start,end-1));
}
return dp[start][end] = ans;
}
// recursive
int longestPanidromeSunsequence(string s,int start,int end){
if(start == end) return 1;
if(start > end) return 0;
int ans = 0;
// if first and last letter is same
if(s[start] == s[end]){
ans = longestPanidromeSunsequence(s,start + 1,end - 1) + 2;
}
// if first and last letter is different
else{
ans = max(longestPanidromeSunsequence(s,start+1,end),longestPanidromeSunsequence(s,start,end-1));
}
return ans;
}
int main(){
string s;
cin>>s;
memset(dp,0,sizeof(dp));
int n = s.length();
cout<<longestPanidromeSunsequence(s,0,n-1)<<endl;
cout<<longestPanidromeSunsequenceTDDP(s,0,n-1)<<endl;
cout<<longestPanidromeSunsequenceBUDP(s);
return 0;
}