-
Notifications
You must be signed in to change notification settings - Fork 0
/
common-child.cpp
55 lines (43 loc) · 976 Bytes
/
common-child.cpp
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
45
46
47
48
49
50
51
52
53
54
55
#include <bits/stdc++.h>
using namespace std;
/*
* Complete the 'commonChild' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. STRING s1
* 2. STRING s2
*/
int commonChild(string s1, string s2) {
int n = s1.size();
int m = s2.size();
vector<vector<int>>dp(n+1,vector<int>(m+1,0));
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
if(i == 0 || j == 0) continue;
if(s1[i-1] == s2[j-1])
{
dp[i][j] = dp[i-1][j-1] + 1;
}
else
{
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
return dp[n][m];
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
string s1;
getline(cin, s1);
string s2;
getline(cin, s2);
int result = commonChild(s1, s2);
fout << result << "\n";
fout.close();
return 0;
}