Advertisement
Falak_Ahmed_Shakib

1143. Longest Common Subsequence

Sep 6th, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1.  
  2.  
  3. class Solution {
  4. public:
  5. string s1,s2;
  6. int n,m;
  7. int dp[1006][1006];
  8. int lcs(int i,int j)
  9. {
  10. if(i==n or j==m)return 0;
  11.  
  12. if(dp[i][j]!=-1)return dp[i][j];
  13.  
  14. int ret;
  15. if(s1[i]==s2[j])
  16. {
  17. ret=1+lcs(i+1,j+1);
  18. }else{
  19.  
  20. ret=max(lcs(i+1,j),lcs(i,j+1));
  21.  
  22. }
  23.  
  24.  
  25. dp[i][j]=ret;
  26.  
  27. return dp[i][j];
  28.  
  29.  
  30. }
  31. int longestCommonSubsequence(string text1, string text2) {
  32.  
  33. s1=text1;
  34. s2=text2;
  35. n=s1.size();
  36. m=s2.size();
  37. memset(dp,-1,sizeof(dp));
  38. return lcs(0,0);
  39.  
  40. }
  41.  
  42. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement