uva 514

原题

好经典的一道题, 虽然见过几次了, 不过一开始居然还想不起来..

用STL的堆栈模拟就行了, 关键做法就是对输入的序列从头扫描, 

如果扫描到的数不是栈顶元素就压栈(因为不是栈顶元素肯定就不是现在这步出来的元素嘛 )

如果是就扫描下一个数, 以此类推

如果扫描完尾部最后一个数, 堆栈不是空的, 说明出栈序列不合法

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <cassert>
#include <iomanip>

using namespace std;
const int MAXN 	= 100000;
const int BASE 	= 100000000;
typedef long long LL;

/*
uva 210
关键 : 用堆栈模拟堆栈 

*/
stack<int>  s;
vector<int> vec;

int main(){
//	freopen("input2.txt","r",stdin);
	int N,M,n;
	while( scanf("%d",&N) && N>0 ){
		while( cin>>n && n>0 ){
			vec.clear();
			vec.push_back(n);
			while( !s.empty() ) s.pop();
			for( int i=1; i<N; i++){
				cin >> n;
				vec.push_back(n);
			}
			int ptr = 0, num = 0;
			s.push(++num);
			while( !(ptr>=N || num>N) ){
				if( !s.empty() && s.top()==vec[ptr] ){
					s.pop();
					ptr++;
				}else{
					s.push(++num);
				}
			}
			if( s.empty() ) cout << "Yes" << endl;
			else cout << "No" << endl;
		}
		cout << endl;
	}
	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值