fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define endl '\n'
  5.  
  6. void solve(){
  7. ll n = 1e9;
  8. string s;cin>>s;
  9. stack<char> st;
  10. vector<int>v;
  11. map<char, ll> mp;
  12. ll sum = 1;
  13. for(int i = 0; i < s.size(); ++i){
  14. if(s[i]>='2' && s[i] <= '9'){
  15. v.push_back(s[i] - '0');
  16. continue;
  17. }
  18. if(s[i]!=')'){
  19. st.push(s[i]);
  20. continue;
  21. }
  22. ll sum = 1;
  23. for(auto x : v){
  24. sum *= x;
  25. sum %= n;
  26. }
  27. v.pop_back();
  28. while(st.top()!='('){
  29. int ch = st.top();
  30. mp[ch] += sum;
  31. mp[ch] %= n;
  32. st.pop();
  33. }
  34. st.pop();
  35. }
  36. while(!st.empty()){
  37. int ch = st.top();
  38. mp[ch] += sum;
  39. mp[ch] %= n;
  40. st.pop();
  41. }
  42. ll X = 0, y = 0;
  43. for(auto x : mp){
  44. X%=n;
  45. y%=n;
  46. if(x.first=='N'){
  47. y -= x.second;
  48. y += n;
  49. } else if(x.first=='S'){
  50. y += x.second;
  51. } else if(x.first=='E'){
  52. X += x.second;
  53. } else{
  54. X -= x.second;
  55. X += n;
  56. }
  57. X%=n;
  58. y%=n;
  59. }
  60. X += n;y += n;
  61. X%=n;
  62. y%=n;
  63. cout<<X + 1<<" "<<y + 1<<endl;
  64.  
  65. }
  66.  
  67. int main(){
  68. ios_base::sync_with_stdio(false);
  69. cin.tie(NULL);
  70. int t;cin>>t;
  71. for(int i = 0; i < t; ++i){
  72. cout<<"Case #"<<i+1<<": ";
  73. solve();
  74. }
  75. return 0;
  76. }
Success #stdin #stdout 0s 4328KB
stdin
Standard input is empty
stdout
Standard output is empty