rt,本来不想发求助帖的,但自己从昨天七点多放学到家到晚上除了摸鱼的时间都在写和调,总是比输出样例多输出一行。晚上睡觉的时候还偷偷地改,还是不行。今天又接着看还是没查出错。
交了一发是 Unaccepted 80。
改的时候看了题解和讨论区里提到的很多种可能错误的方法,但似乎都没踩到那些坑。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXN=1e6+10;
const int MOD=1e9+7;
// stack
ll a[MAXN],t;
void push(ll n){
a[t++]=n;
}
void pop(){
t--;
}
ll top(){
return a[t-1];
}
ll size(){
return t;
}
bool empty(){
return t==0;
}
ll top2(){
return a[t-2];
}
//
string str[MAXN],x;
ll num[MAXN],j;
inline __attribute__((always_inline)) void check1(ll z){
if(abs(z)>=1e9){
cout<<"ERROR\n";
return ;
}
}
inline __attribute__((always_inline)) void check2(){
if(size()<2){
cout<<"ERROR\n";
return ;
}
}
inline __attribute__((always_inline)) void check3(){
if(empty()){
cout<<"ERROR\n";
return ;
}
}
void cal(ll y){
while(!empty()){
pop();
}
push(y);
for(ll i=1;i<=j;i++){
if(str[i]=="NUM"){
// cin>>y;
check1(num[i]);
push(num[i]);
}
else if(str[i]=="POP"){
check3();
y=0;
check1(y);
pop();
}
else if(str[i]=="INV"){
check3();
y=-top();
check1(y);
pop();
push(y);
}
else if(str[i]=="DUP"){
check3();
y=top();
check1(y);
push(y);
}
else if(str[i]=="SWP"){
check3();
y=top();
check2();
a[t-1]=top2();
a[t-2]=y;
}
else if(str[i]=="ADD"){
y=top2()+top();
check1(y);
check2();
pop();
pop();
push(y);
}
else if(str[i]=="SUB"){
y=top2()-top();
check1(y);
check2();
pop();
pop();
push(y);
}
else if(str[i]=="MUL"){
y=top2()*top();
check1(y);
check2();
pop();
pop();
push(y);
}
else if(str[i]=="DIV"){
if(top()!=0){
y=top2()/top();
check1(y);
check2();
pop();
pop();
push(y);
}
else{
cout<<"ERROR\n";
return ;
}
}
else if(str[i]=="MOD"){
if(top()!=0){
y=top2()%top();
check1(y);
check2();
pop();
pop();
push(y);
}
else{
cout<<"ERROR\n";
return ;
}
}
// else if(str[i]=="END"){
// break;
// }
}
if(size()==1){
cout<<top()<<"\n";
}
else{
cout<<"ERROR\n";
}
}
int main(){
while(cin>>x){
if(x=="END"){
break;
}
j++;
str[j]=x;
if(x=="NUM"){
ll y;
cin>>y;
num[j]=y;
}
}
ll n;
cin>>n;
for(ll i=1;i<=n;i++){
ll z;
cin>>z;
cal(z);
}
return 0;
}