Advertisement
Saleh127

Light OJ 1013 / DP

Aug 26th, 2022
987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. /***
  2.  created: 2022-08-26-21.32.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. ll dp[35][35];
  17. ll dp1[35][35][100];
  18. string x,y;
  19. ll n,m;
  20.  
  21. ll solve(ll i,ll j)
  22. {
  23.      if(i>=n || j>=m) return 0;
  24.  
  25.      if(dp[i][j]!=-1) return dp[i][j];
  26.  
  27.      ll ans=0;
  28.  
  29.      if(x[i]==y[j])
  30.      {
  31.           ans=max(ans,1+solve(i+1,j+1));
  32.      }
  33.      else
  34.      {
  35.           ans=max(ans,solve(i,j+1));
  36.           ans=max(ans,solve(i+1,j));
  37.      }
  38.  
  39.      return dp[i][j]=ans;
  40. }
  41.  
  42. ll solve1(ll i,ll j,ll l)
  43. {
  44.     if(i==n && j!=m) return solve1(i,j+1,l-1);
  45.     if(i!=n && j==m) return solve1(i+1,j,l-1);
  46.     if(i==n && j==m && l==0) return 1;
  47.     if(i==n && j==m && l) return 0;
  48.     if(dp1[i][j][l]!=-1) return dp1[i][j][l];
  49.  
  50.     ll ans=0;
  51.  
  52.     if(x[i]==y[j])
  53.     {
  54.         ans=solve1(i+1,j+1,l-1);
  55.     }
  56.     else
  57.     {
  58.         ans=solve1(i+1,j,l-1) + solve1(i,j+1,l-1);
  59.     }
  60.  
  61.     return dp1[i][j][l]=ans;
  62. }
  63.  
  64. int main()
  65. {
  66.     ios_base::sync_with_stdio(0);
  67.     cin.tie(0);
  68.     cout.tie(0);
  69.  
  70.     test
  71.     {
  72.         ll i,j,k,len=0;
  73.  
  74.         cin>>x>>y;
  75.  
  76.         n=x.size();
  77.         m=y.size();
  78.  
  79.         memset(dp,-1,sizeof dp);
  80.         memset(dp1,-1,sizeof dp1);
  81.  
  82.         len=n+m-solve(0,0);
  83.  
  84.         cout<<"Case "<<cs<<": "<<len<<" "<<solve1(0,0,len)<<nl;
  85.     }
  86.  
  87.     get_lost_idiot;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement