0% found this document useful (0 votes)
2 views3 pages

Ada Practical Shrishti

The document contains a C++ program that calculates the Longest Common Subsequence (LCS) between two input strings. It prompts the user to enter two strings, computes the LCS using dynamic programming, and outputs both the length of the LCS and the LCS itself. An example output is provided, showing the LCS of the strings 'DFKJRNBSL' and 'OKEDUNBSDL' as 'KNBSL' with a length of 5.

Uploaded by

rahemram62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Ada Practical Shrishti

The document contains a C++ program that calculates the Longest Common Subsequence (LCS) between two input strings. It prompts the user to enter two strings, computes the LCS using dynamic programming, and outputs both the length of the LCS and the LCS itself. An example output is provided, showing the LCS of the strings 'DFKJRNBSL' and 'OKEDUNBSDL' as 'KNBSL' with a length of 5.

Uploaded by

rahemram62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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 = X.length();
int n = Y.length();

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

You might also like