-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS.cpp
More file actions
67 lines (57 loc) · 1.21 KB
/
LCS.cpp
File metadata and controls
67 lines (57 loc) · 1.21 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
#include <bits/stdc++.h>
#define lli long long int
#define endl "\n"
#define debug(n) cout<<n<<endl
#define debug2(a, b) cout<<a<<" "<<b<<endl;
#define forn(i, in, fin) for(int i = in; i<fin; i++)
#define all(v) v.begin(), v.end()
#define fastIO(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
string s, t;
lli n, q;
vector< vector< lli > > dp;
//int dp[5010][5010];
lli LCS(lli i, lli j)
{
if(i == n || j ==q) return 0;
if(dp[i][j] != -1) return dp[i][j];
if(s[i] == t[j])
{
return dp[i][j] = LCS(i+1, j+1) + 1;
}
return dp[i][j] = max(LCS(i+1, j), LCS(i, j+1));
}
void rec(lli &i, lli &j)
{
if(i == n || j == q) return;
if(s[i] == t[j])
{
cout<<i<<" "<<j<<endl;
i++, j++;
rec(i, j);
return;
}
if(dp[i+1][j] > dp[i][j+1])
{
i++;
rec(i, j);
}
else
{
j++;
rec(i, j);
}
return;
}
int main()
{
fastIO();
cin>>s>>t;
n = s.length();
q = t.length();
dp.assign(n+1, vector<lli>(q+1, -1));
cout<< LCS(0,0) << endl;
lli i = 0, j = 0;
rec(i,j);
return 0;
}