Advertisement
Saleh127

Light OJ 1420 / DP

Feb 21st, 2023
793
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. /***
  2.  created: 2023-02-21-19.32.46
  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 all(we) we.begin(),we.end()
  13. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  14. #define nl '\n'
  15.  
  16. const ll mod=1e9+7;
  17. string a,b,c;
  18.  
  19. ll dp[101][101][101][3],v[101][101][101][3];
  20. ll ts=1,s1,s2,s3;
  21.  
  22. ll solve(ll ai,ll bi,ll ci,ll f)
  23. {
  24.     if(ci==s3) return 1ll;
  25.  
  26.     if(v[ai][bi][ci][f]==ts) return dp[ai][bi][ci][f];
  27.  
  28.     ll ans=0;
  29.  
  30.     if(f==0)
  31.     {
  32.         ans+=solve(ai,bi,ci,1);
  33.         if(ans>=mod) ans-=mod;
  34.         ans+=solve(ai,bi,ci,2);
  35.         if(ans>=mod) ans-=mod;
  36.     }
  37.     else if(f==1)
  38.     {
  39.         if(ai==s1) return 0;
  40.         if(a[ai]==c[ci])
  41.         {
  42.             ans+=solve(ai+1,bi,ci+1,0);
  43.             if(ans>=mod) ans-=mod;
  44.         }
  45.         ans+=solve(ai+1,bi,ci,1);
  46.         if(ans>=mod) ans-=mod;
  47.     }
  48.     else
  49.     {
  50.         if(bi==s2) return 0;
  51.         if(b[bi]==c[ci])
  52.         {
  53.             ans+=solve(ai,bi+1,ci+1,0);
  54.             if(ans>=mod) ans-=mod;
  55.         }
  56.         ans+=solve(ai,bi+1,ci,2);
  57.         if(ans>=mod) ans-=mod;
  58.     }
  59.     if(ans>=mod) ans-=mod;
  60.  
  61.     v[ai][bi][ci][f]=ts;
  62.     return dp[ai][bi][ci][f]=ans;
  63. }
  64.  
  65. int main()
  66. {
  67.     ios_base::sync_with_stdio(0);
  68.     cin.tie(0);
  69.     cout.tie(0);
  70.  
  71.     test
  72.     {
  73.         cin>>a>>b>>c;
  74.         s1=a.size(),s2=b.size(),s3=c.size();
  75.         cout<<"Case "<<cs<<": "<<solve(0ll,0ll,0ll,0ll)<<nl;
  76.         ts++;
  77.     }
  78.  
  79.     return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement