Vjudge题目地址,按紫书上模拟即可,但这里有一个知识点,栈混洗的判断,栈混洗的判断只需要在模拟时把压栈顺序改为要求的初始栈序列的顺序即可。
#include<cstdio>
#include<iostream>
#include<stack>
using namespace std;
const int maxn = 1010;
int a[maxn];//要求的出栈顺序
int main(){
//freopen("input.txt", "r", stdin);
int n;
while(~scanf("%d", &n) && n){
while(~scanf("%d", &a[1]) && a[1] != 0){
for(int i = 2; i <= n; i++)scanf("%d", &a[i]);
stack<int> s; int j = 1;//输出栈的下标
for(int i = 1; i <= n; i++){//模拟栈混洗
s.push(i);//按初始栈的顺序入栈(这里就是1~n,如果是判断栈混洗,就按初始栈的顺序入栈)
while(!s.empty() && s.top() == a[j]){s.pop(); j++;}
}
printf( (s.empty() && j == n+1) ? "Yes\n" : "No\n" );
}
printf("\n");
}
return 0;
}