UVa - 514 Rails(栈模拟)

9 篇文章 0 订阅
8 篇文章 0 订阅

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19641

#include <iostream>
#include <stack>

using namespace std;
/*************************************************************************************************************
            题意:给定两个序列A,B,判断B是否可由A经过进站出站得到
            思路:
            1,利用栈模拟,将可以的情况写出来
            2,注意按要求输入输出
*************************************************************************************************************/
int a[1005];

int main()
{
    int n;
    while(cin>>n,n!=0)
    {
        while(cin >> a[1] && a[1]){
            stack<int> s;
            for(int i = 2;i <= n;i ++)
                cin>>a[i];

            int A=1,B=1,flag=1;
            while(B <= n){
                if(A == a[B]){
                    A++;
                    B++;
                }
                else if(!s.empty() && s.top() == a[B]){
                    s.pop();
                    B++;
                }
                else if(A <= n)
                    s.push(A++);
                else{
                    flag=0;
                    break;
                }
            }
            cout<< (flag ? "Yes" : "No") << endl;
        }
        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值