-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS.cpp
More file actions
96 lines (83 loc) · 2.4 KB
/
LCS.cpp
File metadata and controls
96 lines (83 loc) · 2.4 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
//BAAP IZ HERE//
/*
KABILAN . M
PRATICE IS THE ONLY SHORTCUT TO SUCCESS
IIT GUWAHATI
*/
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-funroll-all-loops,-fpeel-loops,-funswitch-loops")
using namespace std;
#define ff first
#define ss second
#define sb substr
#define all(v) v.begin(),v.end()
#define ll long long int
#define ull unsigned long long int
#define pi pair <ll, ll>
#define ppi pair<ll,pair<ll,ll>>
#define db double
#define bits(n) __builtin_popcountll(n)
#define prec(n) fixed<<setprecision(n)
#define pb(x) push_back(x)
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ln "\n"
#define vi vector<ll>
#define forl(i,n) for(ll i = 0; i < n; i++)
#define deb(x) cout << #x << "=" << x << endl
#define p0(a) cout << a << " "
#define p1(a) cout << a << ln
#define p2(a,b) cout << a << " " << b << ln
#define p3(a,b,c) cout << a << " " << b << " " << c << ln
#define p4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << ln
const ll mod = 1e9+7; //998244353;
const ll INF = 1e18L+5;
void max_self(int &x,int y){
x = max(x,y);
}
int dp[3001][3001]{}; // If defined in main it leads to segmentation fault
// HOWEVER, defined up here is fine???
void solve() {
//cout<<"NEVER GIVE UP"<<ln;
string s, t;
cin >> s >> t;
// Attempt at solution in editorial
for(int i = 1; i <= s.size(); i++){
for(int j = 1; j <= t.size(); j++){
if(s[i-1] == t[j-1]){
dp[i][j] = 1 + dp[i-1][j-1];
} else{
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
// traversing from bottom right till top left to find all the matches
size_t n = s.size(), m = t.size();
string ret;
while(n >= 1 && m >= 1){
if(s[n-1] == t[m-1]){
ret = s[n-1] + ret;
n--; m--;
} else if(dp[n-1][m] > dp[n][m-1]){
n--;
} else{
m--;
}
}
p1(ret);
return;
}
int main(){
fast;
ll t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}