-
Notifications
You must be signed in to change notification settings - Fork 0
/
F_Lcs.cpp
190 lines (157 loc) · 3.82 KB
/
F_Lcs.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
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
/*
* *
* * *
* * *
* *
**********************************************
* THINK TWICE, * * Dknite *
* CODE ONCE. * * *
***************** ****************
*/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define F first
#define S second
#define pb push_back
#define si set <int>
#define vi vector <int>
#define pii pair <int, int>
#define vpi vector <pii>
#define vpp vector <pair<int, pii>>
#define mii map <int, int>
#define mpi map <pii, int>
#define spi set <pii>
#define endl "\n"
#define sz(x) ((int) x.size())
#define all(p) p.begin(), p.end()
#define double long double
#define que_max priority_queue <int>
#define que_min priority_queue <int, vi, greater<int>>
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a) for(auto x : a) cout << x << " "; cout << endl
#define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y) for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
#define FOR(i,a,b) for (int i = a; i < b; i++)
inline int power(int a, int b)
{
int x = 1;
while (b)
{
if (b & 1) x *= a;
a *= a;
b >>= 1;
}
return x;
}
// O(logN) -> __gcd(a,b);
// int gcd(int a,int b)
// {
// if(b==0) return a;
// return gcd(b,a%b);
// }
// negative mod
inline int Nmode(int x,int m)
{
x = x%m;
if (x < 0) x += m;
return x;
}
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
const char* comma = strchr (names + 1, ',');
cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}
// const int N = 200005;
/* void sieve()
{
is_prime[0]=is_prime[1] = true;
for(int i=2;i<=N;i++)
{
if(is_prime[i]==false && i*i<=N)
{
for(int j = i*i;j<=N;j+=i)
{
is_prime[j]= true;
}
}
}
}
*/
string s,t;
int n,m;
int dp[3001][3001];
int rec(int i,int j)
{
// base case
if(i>=n || j>=m)
{
return 0;
}
// cache and check
if(dp[i][j]!=-1)
{
return dp[i][j];
}
// transition / comput
int ans = 0;
ans = max({rec(i+1,j),rec(i,j+1)});
if(s[i]==t[j])
{
ans = max(ans,1+rec(i+1,j+1));
}
// save and return
return dp[i][j] = ans;
}
// to print the solution -> we have to find to find out that
// which transition led to the transition of rec(i+1,j+1) because we included that only.
string sol="";
void printS(int i,int j)
{
if(i>=n || j>=m)
{
return;
}
if(rec(i,j)==rec(i+1,j))
{
return printS(i+1,j);
}
else if(rec(i,j)==rec(i,j+1))
{
return printS(i,j+1);
}
// if(rec(i,j)==1 + rec(i+1,j+1),-> this only can happen when we include the current
// value with the previous value)
else if(rec(i,j)== 1 + rec(i+1,j+1))
{
sol += s[i];
printS(i+1,j+1);
}
}
void solve() {
cin >> s >> t;
n = s.size();
m = t.size();
memset(dp,-1,sizeof(dp));
rec(0,0);
printS(0,0);
cout << sol << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
clock_t z = clock();
int t = 1;
// cin >> t;
while (t--) solve();
cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
return 0;
}