社区讨论

爆0求助

P4387【深基15.习9】验证栈序列参与者 3已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@lw0i9qfx
此快照首次捕获于
2024/05/10 17:59
2 年前
此快照最后确认于
2024/05/10 20:27
2 年前
查看原帖
其他帖子爆0的样例我都能过,但是还是爆0
CPP
#include <bits/stdc++.h>   
using namespace std;  
int up[100100];
queue<int> qu;//下边的 
stack<int> s; //上边入栈的  
bool judge(){ //判断剩下的是否一一对应 
	while(!qu.empty()){
		if(qu.front()!=s.top()){
			return false;
		}
		qu.pop();
		s.pop();
	}
	return true;
} 
int main()  
{  
   int n; cin>>n;
   while(n--){
   	int nx;
    cin>>nx;
    for(int i=1;i<=nx;i++){ //上边暂存 
    	cin>>up[i];
    }
    int nnx=nx;
    while(nnx--){ //下边入队 
    	int y;
    	cin>>y;
    	qu.push(y);
    }
    for(int i=1;i<=nx;i++){ //上边入栈 
        while((!s.empty())&&(s.top()==qu.front())){
    		s.pop();
    		qu.pop();
    	}
    	s.push(up[i]);
    	if(s.top()==qu.front()){
    		s.pop();
    		qu.pop();
    	}
    }
    bool res=judge(); //判断剩下的元素是否一一对应 
    //清空栈和队列
	while(!qu.empty()){
		qu.pop();
	} 
	while(!s.empty()){
		s.pop();
	}
	//输出 
    if(res){
    	cout<<"YES"<<endl;
    }else{
    	cout<<"NO"<<endl;
    }
    
   }
   return 0;  
}

回复

2 条回复,欢迎继续交流。

正在加载回复...