Skip to content

Commit 320986c

Browse files
Merge pull request #1 from shivamsahu55/master
ADDED FILE
2 parents 05fc654 + d7a6bae commit 320986c

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

Diff for: LCS_using_DP.txt

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
#include<bits/stdc++.h>
2+
#include <vector>
3+
using namespace std;
4+
5+
6+
#define f(i,n) for(int i=0;i<n;i++)
7+
#define FOR(i,a,b) for(int i=a;i<b;i++)
8+
#define ff first
9+
#define ss second
10+
#define int long long
11+
#define pb push_back
12+
#define mp make_pair
13+
#define pii pair<int,int>
14+
#define vi vector<int>
15+
#define mii map<int,int>
16+
#define pqb priority_queue<int>
17+
#define pqs priority_queue<int,vi,greater<int> >
18+
#define setbits(x) __builtin_popcountll(x)
19+
#define zrobits(x) __builtin_ctzll(x)
20+
#define mod 1000000007
21+
#define inf 1e18
22+
#define ps(x,y) fixed<<setprecision(y)<<x //works with float
23+
#define mk(arr,n,type) type *arr=new type[n];
24+
#define w(x) int x; cin>>x; while(x--)
25+
#define all(x) x.begin(),x.end()
26+
#define rt return
27+
#define br break
28+
#define ct continue
29+
#define elif else if
30+
#define arrin(a,n) for(int i=0;i<n;i++)cin>>a[i]
31+
#define arrout(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
32+
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
33+
34+
35+
36+
37+
void c_p_c()
38+
{
39+
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
40+
#ifndef ONLINE_JUDGE
41+
freopen("input.txt", "r", stdin);
42+
freopen("out.txt", "w", stdout);
43+
#endif
44+
}
45+
46+
int power(int a, int x) {
47+
48+
int temp = a;
49+
int ans = 1;
50+
// cout<<temp;
51+
while (x) {
52+
if (x & 1) {
53+
ans = (ans * temp) % mod;
54+
// cout<<ans;
55+
}
56+
57+
temp = (temp * temp) % mod;
58+
//cout<<temp<<" ";
59+
x >>= 1;
60+
//cout<<x<<" ";
61+
62+
63+
64+
}
65+
//cout << ans;
66+
67+
return ans % mod;
68+
}
69+
// bool cmp(vector<pair<int, int>> a, vector<pair<int, int>> b) {
70+
// return a.second < b.second;
71+
// }
72+
73+
74+
int getMinDifferenceSubsetSumArrayPartition(int arr[], int n) {
75+
if (n == 0) {
76+
return -1;
77+
}
78+
79+
int sumOfArray = 0;
80+
for (int i = 0; i < n; i++) {
81+
sumOfArray = sumOfArray + arr[i];
82+
}
83+
84+
int sum = sumOfArray / 2;
85+
86+
// bool[][] mat = new boolean[n][sum + 1];
87+
bool mat[n][sum + 1];
88+
89+
90+
for (int i = 0; i < n; i++) {
91+
mat[i][0] = true;
92+
}
93+
94+
for (int j = 0; j <= sum; j++) {
95+
if (j == arr[0]) {
96+
mat[0][j] = true;
97+
}
98+
}
99+
100+
for (int i = 1; i < n; i++) {
101+
for (int j = 1; j <= sum; j++) {
102+
103+
if (mat[i - 1][j]) {
104+
mat[i][j] = true;
105+
} else {
106+
if (j >= arr[i]) {
107+
mat[i][j] = mat[i - 1][j - arr[i]];
108+
}
109+
}
110+
}
111+
}
112+
113+
int lastRow = n - 1;
114+
int firstPartitionSum = -1;
115+
116+
for (int j = sum; j >= 0; j--) {
117+
if (mat[lastRow][j]) {
118+
firstPartitionSum = j;
119+
break;
120+
}
121+
}
122+
123+
int secondPartitionSum = sumOfArray - firstPartitionSum;
124+
125+
return abs(firstPartitionSum - secondPartitionSum);
126+
127+
}
128+
129+
130+
131+
132+
133+
int32_t main()
134+
{
135+
//c_p_c();
136+
string a, b; cin >> a >> b;
137+
int m = a.length();
138+
int n = b.length();
139+
int arr[m + 1][n + 1];
140+
f(i, m+1) {
141+
arr[i][0] = 0;
142+
}
143+
f(i, n+1) {
144+
arr[0][i] = 0;
145+
}
146+
a.insert(0, "0");
147+
b.insert(0, "0");
148+
149+
string ans="";
150+
151+
for (int i = 1; i <= m; i++) {
152+
for (int j = 1; j <= n; j++) {
153+
154+
if (a.at(i) == b.at(j)) {
155+
arr[i][j] = arr[i - 1][j - 1] + 1;
156+
// ans += a.at(i);
157+
}
158+
159+
else {
160+
arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]);
161+
}
162+
163+
164+
165+
}
166+
}
167+
168+
169+
170+
int len= arr[m][n];
171+
int x=n;
172+
int y=m;
173+
174+
while(x>0 and y>0){
175+
if(a.at(y)==b.at(x))
176+
{
177+
178+
char sub=a.at(y);
179+
ans+=sub;
180+
y--;
181+
x--;
182+
}
183+
else if(arr[y-1][x]>arr[y][x-1]){
184+
y--;
185+
}
186+
else{
187+
x--;
188+
}
189+
}
190+
reverse(ans.begin(),ans.end());
191+
cout<<ans;
192+
}
193+
194+
195+
196+
197+
198+
199+
200+
201+
202+

0 commit comments

Comments
 (0)