Skip to content

Commit a9d22d9

Browse files
authored
Add files via upload
1 parent 5c336c5 commit a9d22d9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

LCS.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
3+
#include <bits/stdc++.h>
4+
#define ll long long
5+
#define ull unsigned long long
6+
#define endl '\n'
7+
#define pb push_back
8+
#define mp make_pair
9+
#define sp(x) fixed<<setprecision(x)
10+
#define p_q priority_queue
11+
#define fast_io ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
12+
using namespace std;
13+
14+
int lcs(string x, string y, int m, int n)
15+
{
16+
int dp[m+1][n+1];
17+
18+
for(int i=0;i<=m;i++)
19+
{
20+
for(int j=0;j<=n;j++)
21+
{
22+
if(i==0 || j==0)
23+
dp[i][j] = 0;
24+
25+
else if(x[i-1] == y[j-1])
26+
dp[i][j] = 1 + dp[i-1][j-1];
27+
28+
else
29+
dp[i][j] = max( dp[i][j-1], dp[i-1][j] );
30+
}
31+
}
32+
33+
return dp[m][n];
34+
}
35+
36+
int main()
37+
{
38+
39+
fast_io;
40+
41+
string x,y;
42+
cin>>x>>y;
43+
44+
int m = x.length(), n = y.length();
45+
46+
cout<<lcs(x,y,m,n)<<endl;
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)