-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21 Print SCS.cpp
More file actions
102 lines (86 loc) · 1.63 KB
/
21 Print SCS.cpp
File metadata and controls
102 lines (86 loc) · 1.63 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
/* Author Kartik Shukla */
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
typedef long long int ll;
#define mod 998244353
#define MOD 1000000007
#define PI 3.14159265358
#define inf 1e9
#define INF 1e18
int lcs(string x,string y,int n,int m)
{
int dp[n+1][m+1];
vector<char> a;
int res = -1;
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
if(i==0 || j==0)
dp[i][j] = 0;
else if( x[i-1] == y[j-1])
{
dp[i][j] = 1 + dp[i-1][j-1];
//a.push_back(x[i-1]);
}
else
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
int idx = dp[n][m];
string scsp;
int i = n,j = m;
while(i>0 && j>0)
{
if(x[i-1] == y[j-1])
{
scsp.push_back(x[i-1]);
i--;
j--;
idx--;
}
else
{
if(dp[i-1][j]>dp[i][j-1])
{
scsp.push_back(x[i-1]);
i--;
}
else
{
scsp.push_back(y[j-1]);
j--;
}
}
}
while(i>0)
{
scsp.push_back(x[i-1]);
i--;
}
while(j>0)
{
scsp.push_back(y[j-1]);
j--;
}
reverse(scsp.begin(),scsp.end());
cout<<scsp<<endl;
return dp[n][m];
}
void solve ()
{
string x,y;
cin>>x;
cin>>y;
int n = x.length();
int m = y.length();
cout<<lcs(x,y,n,m)<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}