forked from supershivam13/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS_using_DP.txt
More file actions
202 lines (147 loc) · 3.8 KB
/
LCS_using_DP.txt
File metadata and controls
202 lines (147 loc) · 3.8 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include<bits/stdc++.h>
#include <vector>
using namespace std;
#define f(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x //works with float
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define all(x) x.begin(),x.end()
#define rt return
#define br break
#define ct continue
#define elif else if
#define arrin(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define arrout(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
int power(int a, int x) {
int temp = a;
int ans = 1;
// cout<<temp;
while (x) {
if (x & 1) {
ans = (ans * temp) % mod;
// cout<<ans;
}
temp = (temp * temp) % mod;
//cout<<temp<<" ";
x >>= 1;
//cout<<x<<" ";
}
//cout << ans;
return ans % mod;
}
// bool cmp(vector<pair<int, int>> a, vector<pair<int, int>> b) {
// return a.second < b.second;
// }
int getMinDifferenceSubsetSumArrayPartition(int arr[], int n) {
if (n == 0) {
return -1;
}
int sumOfArray = 0;
for (int i = 0; i < n; i++) {
sumOfArray = sumOfArray + arr[i];
}
int sum = sumOfArray / 2;
// bool[][] mat = new boolean[n][sum + 1];
bool mat[n][sum + 1];
for (int i = 0; i < n; i++) {
mat[i][0] = true;
}
for (int j = 0; j <= sum; j++) {
if (j == arr[0]) {
mat[0][j] = true;
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= sum; j++) {
if (mat[i - 1][j]) {
mat[i][j] = true;
} else {
if (j >= arr[i]) {
mat[i][j] = mat[i - 1][j - arr[i]];
}
}
}
}
int lastRow = n - 1;
int firstPartitionSum = -1;
for (int j = sum; j >= 0; j--) {
if (mat[lastRow][j]) {
firstPartitionSum = j;
break;
}
}
int secondPartitionSum = sumOfArray - firstPartitionSum;
return abs(firstPartitionSum - secondPartitionSum);
}
int32_t main()
{
//c_p_c();
string a, b; cin >> a >> b;
int m = a.length();
int n = b.length();
int arr[m + 1][n + 1];
f(i, m+1) {
arr[i][0] = 0;
}
f(i, n+1) {
arr[0][i] = 0;
}
a.insert(0, "0");
b.insert(0, "0");
string ans="";
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (a.at(i) == b.at(j)) {
arr[i][j] = arr[i - 1][j - 1] + 1;
// ans += a.at(i);
}
else {
arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]);
}
}
}
int len= arr[m][n];
int x=n;
int y=m;
while(x>0 and y>0){
if(a.at(y)==b.at(x))
{
char sub=a.at(y);
ans+=sub;
y--;
x--;
}
else if(arr[y-1][x]>arr[y][x-1]){
y--;
}
else{
x--;
}
}
reverse(ans.begin(),ans.end());
cout<<ans;
}