-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequenceBottomUp.cpp
More file actions
44 lines (42 loc) · 1.08 KB
/
LongestCommonSubsequenceBottomUp.cpp
File metadata and controls
44 lines (42 loc) · 1.08 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
//I am doing DP problems I have done in bottom up manner these days. this is one of them.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define si(n) scanf("%d",&n)
#define sll(n) scanf("%lld",&n)
#define pin(n) printf("%dn",n)
#define plln(n) printf("%lldn",n)
#define pis(n) printf("%d ",n)
#define plls(n) printf("%lld ",n)
#define rep(i, start, end) for(ll i=start; i<end; i++)
#define repDown(i, start, end) for(ll i=start; i>=end; i--)
#define P pair<int,int>
#define PP pair<P,ll>
#define mod 1000000007
#define MAX 500005
#define s second
#define f first
#define newLine printf("n")
#define mem(A,i) memset(A, i, sizeof(A))
int LCSBottomUp(string A, string B){
int a=A.length();
int b=B.length();
vector<vector<int> > DP(a+2,vector<int>(b+2,0));
repDown(i,a-1,0){
repDown(j,b-1,0){
// cout << i << " " << j << endl;
if(A[i]==B[j]) DP[i][j]=1+DP[i+1][j+1];
else{
DP[i][j]=max(DP[i+1][j], DP[i][j+1]);
}
// cout << DP[i][j] << endl;
}
}
return DP[0][0];
}
int main(){;
string A,B;
cin >> A >> B;
cout << LCSBottomUp(A,B) <<endl;
return 0;
}