-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathLCS.cpp
132 lines (119 loc) · 2.5 KB
/
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
// Rishabh Agarwal
#include <bits/stdc++.h>
#define F first
#define S second
#define MAX 10000003
using namespace std;
typedef long long int ll;
const ll mod = 1e18;
const ll INF= 1e18;
long double PI=3.1415926;
ll power(ll a,ll b){
if(b==0){
return 1;
}
ll temp=power(a,b/2)%mod;
if(b%2==0){
return (temp*temp)%mod;
}
else{
return ((a*temp)%mod*temp)%mod;
}
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
vector<ll>v;
vector<ll>::iterator it;
map<ll,ll>mp;
void primeFactors(ll n)
{
// Print the number of 2s that divide n
while (n % 2 == 0)
{
//cout << 2 << " ";
mp[2]++;
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (ll i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
//cout << i << " ";
mp[i]++;
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2){
mp[n]++;
//cout << n << " ";
}
}
ll dp[3001][3001];
void solve(string a, string b){
ll an=a.length();
ll bn=b.length();
for(ll x=0; x<=an; x++){
for(ll y=0; y<=bn; y++){
dp[x][y]=0;
}
}
for(ll x=1; x<=an; x++){
for(ll y=1; y<=bn; y++){
dp[x][y]=max(dp[x-1][y],dp[x][y-1]);
if(a[x-1]==b[y-1]){
if(dp[x][y]<=dp[x-1][y-1]+1){
dp[x][y]=dp[x-1][y-1]+1;
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
//cin>>t;
t=1;
//ll c=1;
while(t--){
string s,t;
cin>>s>>t;
solve(s,t);
string out="";
ll x=s.length();
ll y=t.length();
while(x>0&&y>0){
if(s[x-1]==t[y-1]){
out+=s[x-1];
x--;
y--;
}
else{
if(dp[x-1][y]>dp[x][y-1]){
x--;
}
else{
y--;
}
}
}
reverse(out.begin(),out.end());
cout<<out<<"\n";
mp.clear();
v.clear();
//cout<<"Case #"<<c<<": "<<ans<<"\n";
//c++;
}
return 0;
}