fork download
  1. /*
  2.  * Author: sr1jan
  3.  * I shall taste the blood...
  4.  *
  5. */
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. #define fi first
  10. #define se second
  11. #define sz(x) ((int)(x).size())
  12. #define all(x) (x).begin(),(x).end()
  13. #define rall(x) (x).rbegin(),(x).rend()
  14. #define mp make_pair
  15. #define pb push_back
  16.  
  17. typedef long long ll;
  18. // global
  19.  
  20. // timer code
  21. /* auto start = chrono::high_resolution_clock::now(); */
  22. /* auto end = chrono::high_resolution_clock::now(); */
  23. /* double time_taken = chrono::duration_cast<chrono::nanoseconds>(end - start).count(); */
  24. /* time_taken *= 1e-9; */
  25. /* cout << time_taken << setprecision(9) << endl; */
  26.  
  27. ll gcd(ll a, ll b) {if(a==0) return b; return gcd(b%a, a);}
  28.  
  29. bool p_se_sort(pair<int,int> a, pair<int,int> b){
  30. return a.second<b.second;
  31. }
  32.  
  33. const int mx = 1e9;
  34.  
  35. void showstack(stack <string> s)
  36. {
  37. while (!s.empty())
  38. {
  39. cout << '\t' << s.top();
  40. s.pop();
  41. }
  42. cout << '\n';
  43. }
  44.  
  45. void solve(){
  46. string s;
  47. int w=1, h=1;
  48. stack <char> st;
  49. st.push('(');
  50. cin>>s;
  51. s+=")";
  52. vector<char> ans;
  53. for(char c: s){
  54. if(c!=')') st.push(c);
  55. else{
  56. vector<char> sub;
  57. while(st.top() != '('){
  58. char ss = st.top();
  59. sub.emplace_back(ss);
  60. st.pop();
  61. }
  62. reverse(all(sub));
  63. st.pop();
  64. if(st.size()<=0){
  65. ans = sub;
  66. break;
  67. }
  68. if(isdigit(st.top())){
  69. int m = (int)(st.top() - 48);
  70. st.pop();
  71. while(m--){
  72. for(int i=0; i<sz(sub); i++){
  73. st.push(sub[i]);
  74. }
  75. };
  76. }
  77. }
  78. }
  79.  
  80. for(char c: ans){
  81. /* cout << c << ' '; */
  82. if(c=='N'&&h>1) h-=1;
  83. else if(c=='N'&&h==1) h = mx;
  84.  
  85. else if(c=='S'&&h<mx) h+=1;
  86. else if(c=='S'&&h==mx) h = 1;
  87.  
  88. else if(c=='E'&&w<mx) w+=1;
  89. else if(c=='E'&&w==mx) w = 1;
  90.  
  91. else if(c=='W'&&w>1) w-=1;
  92. else if(c=='W'&&w==1) w = mx;
  93.  
  94. }
  95. cout << w << ' ' << h << endl;
  96. }
  97.  
  98. int main(){
  99. ios_base::sync_with_stdio(false);
  100. cin.tie(NULL);
  101.  
  102. int t;
  103. cin>>t;
  104. int ti=0;
  105. while(t--){
  106. ++ti;
  107. cout << "Case #" << ti << ": ";
  108. solve();
  109. }
  110.  
  111. }
  112.  
Success #stdin #stdout 0s 4540KB
stdin
4
SSSEEE
N
N3(S)N2(E)N
2(3(NW)2(W2(EE)W))
stdout
Case #1: 4 4
Case #2: 1 1000000000
Case #3: 3 1
Case #4: 3 999999995