您的位置:首页 > 其它

nyoj+stack的简单使用

2015-10-31 18:19 288 查看
点击打开链接
//终于明白了为什么花括号要那样放了。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
stack<int> s1,s2,s3;
int N=0,P=0,Q=0,a=0,b=0;
void solve()
{
int i=0,temp=0;
for(i=0; i<Q; i++){
scanf("%d%d",&a,&b);
if(a==1){
if(!s1.empty()){
temp=s1.top();
s1.pop();
if(b==2){
if(s2.empty()) s2.push(temp);
else{
if(s2.top()<temp){
printf("illegal\n");
return;
}
else s2.push(temp);
}
}
else if(b==3){
if(s3.empty()) s3.push(temp);
else{
if(s3.top()<temp){
printf("illegal\n");
return;
}
else s3.push(temp);
}
}
}
else{
printf("illegal\n");
return;
}
}

if(a==2){
if(!s2.empty()){
temp=s2.top();
s2.pop();
if(b==1){
if(s1.empty()) s1.push(temp);
else{
if(s1.top()<temp){
printf("illegal\n");
return;
}
else s1.push(temp);
}
}
else if(b==3){
if(s3.empty()) s3.push(temp);
else{
if(s3.top()<temp){
printf("illegal\n");
return;
}
else s3.push(temp);
}
}
}
else{
printf("illegal\n");
return;
}
}
if(a==3){
if(!s3.empty()){
temp=s3.top();
s3.pop();
if(b==1){
if(s1.empty()) s1.push(temp);
else{
if(s1.top()<temp){
printf("illegal\n");
return;
}
else s1.push(temp);
}
}
else if(b==2){
if(s2.empty()) s2.push(temp);
else{
if(s2.top()<temp){
printf("illegal\n");
return;
}
else s2.push(temp);
}
}
}
else{
printf("illegal\n");
return;
}
}
}
printf("legal\n");
}
int main()
{
int i=0;
scanf("%d",&N);
while(N--){
scanf("%d%d",&P,&Q);
for(i=P; i>0; i--) s1.push(i);
solve();
while(!s1.empty()) s1.pop();
while(!s2.empty()) s2.pop();
while(!s3.empty()) s3.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: