题目:
思路:
自己想没有想出来…
看图:
看图很容易理解,用了一个原顺序栈,顺序不对栈定不能pop净
代码:
#include <stack>
#include <queue>
#include <stdio.h>
//序列存储到队列中
bool check_valid(std::queue<int> &order){
//存放1-n的栈s
std::stack<int> s;
int n = order.size();
for (int i = 1; i <= n; ++i){
s.push(i);
while(!s.empty()&&s.top()==order.front()){
//如果s不为空,且栈顶和队头相等
s.pop();
order.pop();
}
}
//最后判断
if(!s.empty()){
return false;
}
return true;
}
int main(){
int n;
int train;
scanf("%d", &n);
while(n){
scanf("%d", &train);
while(train){
std::queue<int> order;
order.push(train);
for(int i = 1; i < n; i++){
scanf("%d",&train);
order.push(train);
}
if(check_valid(order)){
printf("Yes\n");
}else{
printf("No\n");
}
scanf("%d", &train);
}
printf("\n");
scanf("%d", &n);
}
return 0;
}
总结:
新的开始,继续加油!