-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.LCS
52 lines (29 loc) · 829 Bytes
/
19.LCS
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
--> 19 Longest common subsequence Recursive
Video Link : https://youtu.be/4Urd0a0BNng
Recursive Method Code :
#include<bits/stdc++.h>
using namespace std;
int longestCommonSubstr (string s1, string s2, int n, int m)
{
// your code here
if(n==0 || m==0)
{
return 0;
}
if(s1[n-1]==s2[m-1])
{
return 1+longestCommonSubstr(s1,s2,n-1,m-1);
}
else
{
return max(longestCommonSubstr(s1,s2,n,m-1) , longestCommonSubstr(s1,s2,n-1,m));
}
}
int main()
{
int n, m; cin >> n >> m;
string s1, s2;
cin >> s1 >> s2;
cout << longestCommonSubstr (s1, s2, n, m) << endl;
return 0;
}