// // File: execute.cc // Author: cteam18 // // Created on November 13, 2011, 10:01 AM // #include #include #include #include #include #include using namespace std; int main(int argc, char** argv) { char command[8]; int n, init, num, a, b, h; scanf("%s", command); vector coms; coms.reserve(100000); vector stack, numcmds; stack.reserve(1000); while(command[0] != 'Q'){ bool bad = false; coms.clear(); numcmds.clear(); int len = 1; while(command[0] != 'E'){ coms.push_back(string(command)); switch(command[0]){ case 'N': scanf("%d", &num); numcmds.push_back(num); ++len; break; case 'P': if(len < 1) bad = true; --len; break; case 'I': if(len < 1) bad = true; break; case 'D': if(command[1] == 'U'){ if(len < 1) bad = true; ++len; } else { if(len < 2) bad = true; --len; } break; case 'S': if(command[1] == 'W'){ if(len < 2) bad = true; } else { if(len < 2) bad = true; --len; } break; case 'A': if(len < 2) bad = true; --len; break; case 'M': if(command[1] == 'U'){ if(len < 2) bad = true; --len; } else { if(len < 2) bad = true; --len; } break; } if(len < 0) bad = true; scanf("%s", command); } if(len != 1) bad = true; scanf("%d", &n); for(int i = 0; i < n; ++i){ scanf("%d", &init); if(bad){ printf("ERROR\n"); continue; } h = 0; bool error = false; stack.clear(); stack.push_back(init); for(int j = 0; j < coms.size() && !error; ++j){ switch(coms[j][0]){ case 'N': stack.push_back(numcmds[h++]); break; case 'P': stack.pop_back(); break; case 'I': stack.back() = -stack.back(); break; case 'D': if(coms[j][1] == 'U'){ stack.push_back(stack.back()); } else { a = stack.back(); if(a == 0){ error = true; continue; } stack.pop_back(); stack.back() = stack.back() / a; } break; case 'S': if(coms[j][1] == 'W'){ a = stack.back(); stack.pop_back(); b = stack.back(); stack.back() = a; stack.push_back(b); } else { a = stack.back(); stack.pop_back(); stack.back() = stack.back() - a; if(abs(stack.back()) > 1000000000){ error = true; continue; } } break; case 'A': a = stack.back(); stack.pop_back(); stack.back() = stack.back() + a; if(abs(stack.back()) > 1000000000){ error = true; continue; } break; case 'M': if(coms[j][1] == 'U'){ a = stack.back(); stack.pop_back(); if(abs(stack.back()) > 1000000000 / abs(a)){ error = true; continue; } stack.back() = stack.back() * a; } else { a = stack.back(); stack.pop_back(); stack.back() = stack.back() % a; } break; } // printf("stack:"); // for(int k = 0; k < stack.size(); ++k) // printf(" %d", stack[k]); // printf("\n"); } if(error || stack.size() != 1){ printf("ERROR\n"); } else { printf("%d\n", stack[0]); } } printf("\n"); scanf("%s", command); } return (0); }