-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcsMy.cpp
More file actions
44 lines (37 loc) · 956 Bytes
/
lcsMy.cpp
File metadata and controls
44 lines (37 loc) · 956 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
37
38
39
40
41
42
43
44
#include<iostream>
#include<cstring>
using namespace std;
int lcs(string s1, string s2){
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
int m = s1.length();
int n = s2.length();
int** dp = new int*[m+1];
for(int i = 0; i <= m; i++){
dp[i] = new int[n+1];
}
for(int i = 0; i <= m; i++){
dp[0][i] = 0;
}
for(int i = 0; i <= n; i++){
dp[i][0] = 0;
}
for(int i = 1; i <= m; i++){
for(int j = 0; j <= n; j++){
if(s1[m-i] = s2[n-j]){
dp[i][j] = 1 + dp[i-1][j-1];
}else{
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
}
int res = dp[m][n];
for(int i = 0; i <= m; i++){
delete [] dp[i];
}
delete [] dp;
return res;
}