Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

added code for longest common subsequence #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions longest_common_subsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <bits/stdc++.h>
using namespace std;

int longestCommonSubsequence(string text1, string text2) {

int n=text1.length()+1;
int m=text2.length()+1;
vector<vector<int>>lcs(n,vector<int>(m,0));
for(int i=n-2;i>=0;i--)
{
for(int j=m-2;j>=0;j--)
{
if(text1[i]==text2[j])
{
lcs[i][j]=lcs[i+1][j+1]+1;
}
else
{
lcs[i][j]=max(lcs[i+1][j],lcs[i][j+1]);
}
}
}
return lcs[0][0];
}
Comment on lines +21 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some concise comments as well, because we want the code to be quite beginner-friendly, i.e., understandable from reading only





Comment on lines +41 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove blank lines


int main()
{
string s1,s2;
cin>>s1>>s2;
cout<<longestCommonSubsequence(s1,s2);
return 0;
}