Skip to content

Commit b157066

Browse files
committed
solved issue #398
1 parent 8ee7e0f commit b157066

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

longestCommonSubsequence.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int lcs(string &X, string &Y, int m, int n)
6+
{
7+
int dp[m+1][n+1];
8+
for(int i=0; i<m+1; ++i)
9+
{
10+
for(int j=0; j<n+1; ++j)
11+
{
12+
if(i == 0 || j == 0)
13+
dp[i][j] = 0;
14+
else if(X[i-1] == Y[j-1])
15+
dp[i][j] = 1 + dp[i-1][j-1];
16+
else
17+
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
18+
}
19+
}
20+
return dp[m][n];
21+
}
22+
23+
int main()
24+
{
25+
//char X[] = "AGGTAB";
26+
//char Y[] = "GXTXAYB";
27+
string X("AGGTAB");
28+
string Y("GXTXAYB");
29+
//int m = strlen(X);
30+
// int n = strlen(Y);
31+
32+
//printf("Length of LCS is %d", lcs( X, Y, m, n ) );
33+
printf("Length of LCS is %d", lcs( X, Y, X.length(), Y.length() ));
34+
35+
return 0;
36+
}

0 commit comments

Comments
 (0)