ADA PRACTICAL
NAME: LAVYA KUMAR BERIWAL
COURSE: BTECH CSE
ROLL NO. : 2301010012
#CODE FOR LONGEST COMMON SUB
SEQUENCE
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
string X, Y;
cout << "Enter first string: ";
cin >> X;
cout << "Enter second string: ";
cin >> Y;
int m = [Link]();
int n = [Link]();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (X[i - 1] == Y[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
string lcs = "";
int i = m, j = n;
while (i > 0 && j > 0) {
if (X[i - 1] == Y[j - 1]) {
lcs = X[i - 1] + lcs;
i--;
j--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}
ABCD
cout << "\nLength of LCS: " << dp[m][n] << endl;
cout << "LCS: " << lcs << endl;
return 0;}
OUTPUT
Enter first string: DFKJRNBSL
Enter second string: OKEDUNBSDL
Length of LCS: 5
LCS: KNBSL