forked from mayankchaudhary26/HacktoberFest-Practice-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS.cpp
More file actions
36 lines (31 loc) Β· 1008 Bytes
/
LCS.cpp
File metadata and controls
36 lines (31 loc) Β· 1008 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int> >
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define pi pair<int, int>
#define pl pair<ll, ll>
#define fora(i, a, n) for(ll i = a; i < n; i++)
#define T int tt; cin >> tt; while(tt--)
int dp[1001][1001];
int helper(string &text1,int i , string &text2,int j){
if(i==text1.length() || j==text2.length())
return 0;
if(dp[i][j]!=-1)
return dp[i][j];
if(text1[i]==text2[j])
return dp[i][j] = 1+helper(text1,i+1,text2,j+1);
else
return dp[i][j] = max(helper(text1,i+1,text2,j),helper(text1,i,text2,j+1));
}
int longestCommonSubsequence(string text1, string text2) {
memset(dp,-1,sizeof(dp));
return helper(text1,0,text2,0);
}
int main(){ ios_base::sync_with_stdio(0); cin.tie(0);
string a,b;
cin>>a>>b;
cout<<longestCommonSubsequence(a,b);
return 0;}