-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS.cpp
49 lines (36 loc) · 883 Bytes
/
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
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define endl '\n'
#define pb push_back
#define mp make_pair
#define sp(x) fixed<<setprecision(x)
#define p_q priority_queue
#define fast_io ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
using namespace std;
int lcs(string x, string y, int m, int n)
{
int dp[m+1][n+1];
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;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];
else
dp[i][j] = max( dp[i][j-1], dp[i-1][j] );
}
}
return dp[m][n];
}
int main()
{
fast_io;
string x,y;
cin>>x>>y;
int m = x.length(), n = y.length();
cout<<lcs(x,y,m,n)<<endl;
return 0;
}