题目描述
代码
看示例理解题意吧……
可以看到这里输入到第5个的时候就已经显示满了,所以容量是4
即这个是最小值的2倍
所以……这就省去了很多复杂情况的讨论……
(一开始就是没看透这条然后搞了半天……)
所以这样非常省事的就是s2空了直接把s1里的全部倒进去就好
那么就让s2容量>s1
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n1,n2;
cin >> n1 >> n2;
stack<string> s1,s2;
int n=max(n1,n2);
n1=n1+n2-n;
n2=n; //让1号为小的栈,作存储位;2号做输出位
string op;
cin >> op;
while(op!="T")
{
if(op=="A")
{
cin >> op; //4
if(s1.size()==n1)
{
if(!s2.empty()) cout << "ERROR:Full" << endl;
else{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
s1.push(op);
}
}
else s1.push(op);
}
else if(op=="D")
{
if(s2.empty())
{
if(s1.empty())
cout << "ERROR:Empty" << endl;
else{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
cout << s2.top() << endl;
s2.pop();
}
}else{
cout << s2.top() << endl;
s2.pop();
}
}
cin >> op;
}
return 0;
}