Facebook
From rIFAT, 2 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 310
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int lcs(string s1,string s2,int m,int n){
  4.     int l[m+1][n+1];
  5.     for(int i=0;i<=m;i++){
  6.         for(int j=0;j<=n;j++){
  7.             if(i==0 || j==0){
  8.                 l[i][j]=0;
  9.             }
  10.             else if(s1[i-1]==s2[j-1]){
  11.                 l[i][j]=1+l[i-1][j-1];
  12.             }
  13.             else{
  14.                 l[i][j]=max(l[i-1][j],l[i][j-1]);
  15.             }
  16.         }
  17.     }
  18.     return l[m][n];
  19. }
  20. int main()
  21. {
  22.     string s1,s2;
  23.     cin>>s1>>s2;
  24.     int m=s1.size();
  25.     int n=s2.size();
  26.     cout<<"Length of LCS is "<<lcs(s1,s2,m,n)<<endl;
  27. }
  28.