Advertisement
aurko96

LCS dp

Nov 21st, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int dp[1500][1500];
  4. string str1,str2;
  5. int k;
  6. int lcs(int x,int y)
  7. {
  8.     if(x==0 || y==0) return 0;
  9.     if(dp[x-1][y-1]!=-1) return dp[x-1][y-1];
  10.     if(str1[x-1]==str2[y-1]) return dp[x-1][y-1]=1+lcs(x-1,y-1);
  11.     else return dp[x-1][y-1]=max(lcs(x,y-1),lcs(x-1,y));
  12. }
  13. int main()
  14. {
  15.     int i,j,n,m,x;
  16.     while(getline(cin,str1))
  17.     {
  18.         getline(cin,str2);
  19.         memset(dp,-1,sizeof(dp));
  20.         n=str1.size();
  21.         m=str2.size();
  22.         x=lcs(n,m);
  23.         cout<<x<<"\n";
  24.     }
  25. }
  26. //ABACDEFG KACDMNEKG
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement