Advertisement
jeff69

lightoj1013

Oct 14th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef int64_t ll;
  5. char a[45],b[45];
  6. string h,u;
  7. int dx=0;
  8. ll dp2[33][33];
  9. ll dp[33][33][64];
  10. int solve(int x,int y)
  11. {
  12.    // cout<<x<<' '<<y<<endl;
  13.     if(x==h.size()&&y==u.size())return 0;
  14.     if(x>h.size()||y>u.size())return 1e5;
  15.          if(dp2[x][y]!=-1)return dp2[x][y];
  16.     int ans=1e5;
  17.     if(x<h.size()&&y<u.size()&&h[x]==u[y])
  18.         ans=1+solve(x+1,y+1);
  19.     ans=min(ans,min(1+solve(x,y+1),1+solve(x+1,y)));
  20.     return dp2[x][y]=ans;
  21. }
  22. ll calc(int x,int y,int l)
  23. {
  24.     ll ans=0;
  25.  
  26.     if(x==h.size()&&y==u.size())return (int)l==dx;
  27.     if(x>h.size()||y>u.size())return 0;
  28.     if(dp[x][y][l]!=-1)return dp[x][y][l];
  29.     if(x<h.size()&&y<u.size()&&h[x]==u[y])
  30.         return dp[x][y][l]=calc(x+1,y+1,l+1);
  31.     ans+=calc(x+1,y,l+1);
  32.     ans+=calc(x,y+1,l+1);
  33.  
  34.     return dp[x][y][l]=ans;
  35. }
  36. int main()
  37. {
  38.     int t;
  39.     cin>>t;
  40.    // freopen("out.txt","w",stdout);
  41.     int g=0;
  42.     while(t--){
  43.             g++;
  44.             memset(dp,-1,sizeof dp);
  45.             memset(dp2,-1,sizeof dp2);
  46.     scanf("%s%s",a,b);
  47.     h=a,u=b;
  48.       dx=solve(0,0);
  49.       printf("Case %d: %d %lld\n",g,dx,calc(0,0,0));
  50.  
  51.     }return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement