Advertisement
Saleh127

UVA 10405

Apr 16th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. ll dp[1005][1005];
  6.  
  7. string a,b;
  8.  
  9. ll lcs(ll i,ll j)
  10. {
  11. if(i<0 || j<0) return 0;
  12.  
  13. if(dp[i][j]!=-1) return dp[i][j];
  14.  
  15. if(a[i]==b[j])
  16. {
  17. dp[i][j]=max(dp[i][j],1+lcs(i-1,j-1));
  18. }
  19. else dp[i][j]=max(lcs(i-1,j),lcs(i,j-1));
  20.  
  21. return dp[i][j];
  22. }
  23.  
  24. int main()
  25. {
  26. ios_base::sync_with_stdio(0);
  27. cin.tie(0);
  28. cout.tie(0);
  29.  
  30.  
  31. while(getline(cin,a))
  32. {
  33. getline(cin,b);
  34. memset(dp,-1,sizeof dp);
  35.  
  36. ll l=a.size()-1;
  37. ll k=b.size()-1;
  38.  
  39. cout<<lcs(l,k)<<endl;
  40. }
  41.  
  42.  
  43. return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement