-
Notifications
You must be signed in to change notification settings - Fork 34
/
LCS(DP)
38 lines (35 loc) · 799 Bytes
/
LCS(DP)
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
#include<iostream>
using namespace std;
int lcs(string s,string t,int **output){
int m = s.size();
int n = t.size();
output[0][0]=0;
for(int i=1;i<=m;i++){
output[i][0]=0;
}
for(int i=1;i<=n;i++){
output[0][i]=0;
}
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(s[m-i]==t[n-j]){
output[i][j] = output[i-1][j-1]+1;
}
else{
output[i][j]=max(output[i-1][j-1],max(output[i][j-1],output[i-1][j]));
}
}
}
return output[m][n];
}
int main(){
string s,t;
cin>>s>>t;
int m = s.size();
int n= t.size();
int **output = new int*[m+1];
for(int i=0;i<=m;i++){
output[i]=new int[n+1];
}
cout<<lcs(s,t,output)<<endl;
}