# 13012: 【原3012】Stack Overflow?

### 题目描述

author: pigoneand 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/3012

## Input Format

Line 1: 一个整数N，表示有N个操作。(1 <= N <= 1,000)

Lines 2..N+1: 每行两个整数，op, num。 op表示操作符，op = 1表示在堆栈中压入一个新的整数num。 op = 2表示在堆栈中弹出最上面存储的整数，并与num进行比较。(1 <= num <= 1,000)

## Output Format

N行。每行一个字符串，表示对应N个操作的运行结果。

## Sample Input

``````5
1 100
1 200
2 200
2 300
2 100
``````

## Sample Output

``````OK
OK
YES
NO
ERROR
``````

## Pangbo13's solution

``````#include<iostream>
using namespace std;

template <class elemType>
class stack{
public:
virtual bool isEmpty() const = 0; //判栈空
virtual void push(const elemType&x) = 0; // 进栈
virtual elemType pop() = 0; // 出栈
virtual elemType top() const = 0; // 取栈顶元素
virtual ~stack() {} //虚析构函数
};
template <class elemType>
class seqStack: public stack<elemType>{
private:
elemType *elem;
int top_p;
int maxSize;
void doubleSpace();
public:
seqStack(int initSize = 10);
~seqStack();
bool isEmpty() const;
void push(const elemType &x);
elemType pop();
elemType top() const;
};

template <class elemType>
seqStack<elemType>::seqStack(int initSize){
elem = new elemType[initSize];
maxSize = initSize ;
top_p = -1;
}
template <class elemType>
seqStack<elemType>::~seqStack(){
delete[] elem;
}
template <class elemType>
bool seqStack<elemType>::isEmpty() const{
}
template <class elemType>
void seqStack<elemType>::push(const elemType &x){
if(top_p == maxSize -1) doubleSpace();
elem[++top_p] = x;
}
template <class elemType>
elemType seqStack<elemType>::pop(){
return elem[top_p--];
}
template <class elemType>
elemType seqStack<elemType>::top() const{
return elem[top_p];
}
template <class elemType>
void seqStack<elemType>::doubleSpace(){
elemType *tmp = elem;
elem = new elemType[2 * maxSize ];
for (int i = 0; i < maxSize; ++i) elem[i] = tmp[i];
maxSize *= 2;
delete [] tmp;
}
int main(){
int N,op,num;
seqStack<int> s;
cin>>N;
for(int i = 0;i<N;i++){
cin>>op>>num;
switch(op){
case 1:
s.push(num);
cout<<"OK"<<endl;
break;
case 2:
if(s.isEmpty()) cout<<"ERROR"<<endl;
else{
if(s.pop() == num) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
}
}
``````

## satgo1546's solution

``````#include <stdio.h>

int a[1005];
int i = 0;

int main(int argc, char *argv[]) {
scanf("%d", &argc);
while (argc--) {
int op, b;
scanf("%d%d", &op, &b);
puts(op == 1 ? a[i++] = b, "OK" : i ? a[--i] == b ? "YES" : "NO" : "ERROR");
}
return 0;
}
``````