Advertisement
Saleh127

UVA 10617 / Edit distance - DP

Apr 15th, 2022 (edited)
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. /***
  2.  created: 2022-04-16-03.32.23
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. string a;
  13. ll dp[80][80];
  14.  
  15. ll solve(ll i,ll j)
  16. {
  17.     if(i>j) return 0;
  18.     if(i==j) return 1;
  19.  
  20.     if(dp[i][j]!=-1) return dp[i][j];
  21.  
  22.     ll ans=0;
  23.  
  24.     if(a[i]==a[j])
  25.     {
  26.         ans=1+solve(i+1,j-1);
  27.     }
  28.  
  29.     ans+=solve(i+1,j);
  30.     ans+=solve(i,j-1);
  31.     ans-=solve(i+1,j-1);
  32.  
  33.     return dp[i][j]=ans;
  34. }
  35.  
  36.  
  37. int main()
  38. {
  39.     ios_base::sync_with_stdio(0);
  40.     cin.tie(0);
  41.     cout.tie(0);
  42.  
  43.  
  44.     test
  45.     {
  46.         cin>>a;
  47.  
  48.         memset(dp,-1,sizeof dp);
  49.  
  50.         cout<<solve(0,a.size()-1)<<nl;
  51.     }
  52.  
  53.  
  54.     get_lost_idiot;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement