Skip to content

Commit e9b34e7

Browse files
authored
Merge pull request #37 from code08-ind/new-branch
Added All The File Of Longest Common Subsequence
2 parents aaaff6d + dbdcf3d commit e9b34e7

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

LongestCommonSubsequence.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <cstring>
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
//finding max value
7+
int max(int a, int b)
8+
{
9+
return (a > b) ? a : b;
10+
}
11+
12+
//finding LCS
13+
int LongestCommonSubsequence(char *str1, char *str2, int x, int y)
14+
{
15+
if (x == 0 || y == 0)
16+
{
17+
return 0;
18+
}
19+
if (str1[x - 1] == str2[y - 1])
20+
{
21+
return (1 + LongestCommonSubsequence(str1, str2, x - 1, y - 1));
22+
}
23+
else
24+
{
25+
return max(LongestCommonSubsequence(str1, str2, x, y - 1), LongestCommonSubsequence(str1, str2, x - 1, y));
26+
}
27+
}
28+
29+
//main function
30+
int main()
31+
{
32+
char str1[] = "cantonment";
33+
char str2[] = "longatone";
34+
int x = strlen(str1);
35+
int y = strlen(str2);
36+
cout << "\nLength of Longest Common Subsequence From Both The Strings Is " << LongestCommonSubsequence(str1, str2, x, y) << endl;
37+
return 0;
38+
}

0 commit comments

Comments
 (0)