Advertisement
Guest User

SWERC 2013/14 - H

a guest
Oct 24th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define MAXN 400000
  4. #define mod 21092013
  5.  
  6. using namespace std;
  7. typedef long long ll;
  8.  
  9. string S_, T_;
  10. vector<char>S;
  11. vector<int> us, nL, nR, nok, ok2s;
  12. int n;
  13.  
  14. ll memo[MAXN];
  15.  
  16.  
  17. // Simple recursion for counting
  18. ll dp(int id){
  19.     if(id == n) return 0; //
  20.     if(memo[id] == -1)
  21.         memo[id] =(ok2s[id] + dp(nL[id]) + dp(nR[id]))%mod;
  22.     return memo[id];
  23. }
  24.  
  25. int main() {
  26.     ios_base::sync_with_stdio(0); cin.tie(0);
  27.    
  28.     int t;
  29.     cin >> t;
  30.     for(int cs = 1; cs <= t; cs++){
  31.         cout << "Case " << cs << ": ";
  32.         cin >> S_ >> T_;
  33.        
  34.         S.clear();
  35.        
  36.         S.push_back(' '); // root of the tree
  37.        
  38.         // get rid of U's in S_
  39.         for(int i = 0; i < (int)S_.size(); i++){
  40.             if(S_[i] == 'U'){ if(S.size() > 1) S.pop_back(); }
  41.             else S.push_back(S_[i]);
  42.         }
  43.        
  44.         int bx = S.size(); // new size of S -> the immutable  instructions
  45.        
  46.         us.clear(); int la = 0;
  47.         nok.assign(T_.size()+1, 0); // nok will keep track of next character that's not 'U'
  48.         for(int i = T_.size()-1; i >= 0; i--){
  49.             if(T_[i] != 'U') nok[i] = ++la;
  50.             else nok[i] = nok[i+1];
  51.         }
  52.        
  53.         // Keeping track of 'U's int T
  54.         for(int i = 0; i < (int)T_.size(); i++){
  55.             if(T_[i] == 'U') us.push_back(nok[i]);
  56.             else S.push_back(T_[i]);
  57.         }
  58.        
  59.         n = S.size();
  60.         int nxtL = n, nxtR = n;
  61.        
  62.        
  63.         ok2s.assign(n+1,1); // keeps track of indexes where you're able to stop
  64.         nL.assign(n+1,n); nR.assign(n+1,n); // position of next letter L/ next letter R
  65.        
  66.         for(int i = n-1; i >= 0; i--){
  67.             nL[i] = nxtL; nR[i] = nxtR;
  68.             if(S[i] == 'L') nxtL = i;
  69.             else nxtR = i;
  70.         }
  71.        
  72.         // nL and nR are different for indexes from S
  73.         for(int i = 0; i < bx-1; i++){
  74.             ok2s[i] = 0;
  75.             nL[i] = nR[i] = n;
  76.             if(S[i+1] == 'L')
  77.                 nL[i] = i+1;
  78.             else
  79.                 nR[i] = i+1;
  80.         }
  81.        
  82.         // Update indexes in S reachable from T
  83.         for(int i = 0; i < (int)us.size(); i++){
  84.             int u_cnt = min(i+2, bx);
  85.            
  86.             ok2s[bx-u_cnt] = 1;
  87.            
  88.             nL[bx - u_cnt] = min(nL[bx - u_cnt], nL[S.size() - us[i]]);
  89.             nR[bx - u_cnt] = min(nR[bx - u_cnt], nR[S.size() - us[i]]);
  90.         }
  91.        
  92.         memset(memo,-1,sizeof memo);
  93.        
  94.        
  95.         cout << dp(0) << '\n';
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement