Advertisement
Saleh127

UVA 12473/ DP - Edit Distance

Aug 5th, 2022
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. /***
  2.  created: 2022-08-05-12.42.30
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16.  
  17. string a,b;
  18.  
  19. ll dp[62][62][62][62];
  20.  
  21. ll solve(ll i,ll j,ll i1,ll j1)
  22. {
  23.     if(i>j || i1>j1) return 0;
  24.  
  25.     if(dp[i][j][i1][j1]!=-1) return dp[i][j][i1][j1];
  26.  
  27.     ll ans=0;
  28.  
  29.     if(a[i]==a[j] && a[j]==b[i1] && b[i1]==b[j1])
  30.     {
  31.         if(i==j || i1==j1) ans=1 + solve(i+1,j-1,i1+1,j1-1);
  32.         else ans= 2 + solve(i+1,j-1,i1+1,j1-1);
  33.     }
  34.     else
  35.     {
  36.         ans=max(ans,solve(i+1,j,i1,j1));
  37.         ans=max(ans,solve(i,j-1,i1,j1));
  38.         ans=max(ans,solve(i,j,i1+1,j1));
  39.         ans=max(ans,solve(i,j,i1,j1-1));
  40.     }
  41.  
  42.     return dp[i][j][i1][j1]=ans;
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48.     ios_base::sync_with_stdio(0);
  49.     cin.tie(0);
  50.     cout.tie(0);
  51.  
  52.  
  53.     test
  54.     {
  55.         cin>>a;
  56.  
  57.         cin>>b;
  58.  
  59.         memset(dp,-1,sizeof dp);
  60.  
  61.         cout<<"Case "<<cs<<": "<<solve(0,a.size()-1,0,b.size()-1)<<nl;
  62.     }
  63.  
  64.  
  65.     get_lost_idiot;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement