Skip to content

Commit

Permalink
Fixing C++ from Longest Common Subsequence (#554)
Browse files Browse the repository at this point in the history
* removing cpp broken code

* Fixing cpp code.

Co-authored-by: Sifat <[email protected]>
  • Loading branch information
jhuynh06 and shhossain authored Oct 29, 2022
1 parent 59e89c1 commit 2ac9ef0
Showing 1 changed file with 2 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,13 @@ int main() {

```


## C++

## CPP
```cpp
// C++ Program for longest common subsequence

#include <iostream>
#include <cstring>
using namespace std;

void lcsAlgo(char *S1, char *S2, int m, int n) {
int LCS_table[m + 1][n + 1];


// Building the mtrix in bottom-up way
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
Expand All @@ -150,11 +144,9 @@ void lcsAlgo(char *S1, char *S2, int m, int n) {
LCS_table[i][j] = max(LCS_table[i - 1][j], LCS_table[i][j - 1]);
}
}

int index = LCS_table[m][n];
char lcsAlgo[index + 1];
lcsAlgo[index] = '\0';

int i = m, j = n;
while (i > 0 && j > 0) {
if (S1[i - 1] == S2[j - 1]) {
Expand All @@ -163,7 +155,6 @@ void lcsAlgo(char *S1, char *S2, int m, int n) {
j--;
index--;
}

else if (LCS_table[i - 1][j] > LCS_table[i][j - 1])
i--;
else
Expand All @@ -173,13 +164,11 @@ void lcsAlgo(char *S1, char *S2, int m, int n) {
// Printing the sub sequences
cout << "S1 : " << S1 << "\nS2 : " << S2 << "\nLCS: " << lcsAlgo << "\n";
}

int main() {
char S1[] = "ACDABCD";
char S2[] = "ADBCA";
int m = strlen(S1);
int n = strlen(S2);

lcsAlgo(S1, S2, m, n);
}

Expand Down

0 comments on commit 2ac9ef0

Please sign in to comment.