UVa - 514 Rails(栈模拟)

来源:互联网 发布:三年大饥荒知乎 编辑:程序博客网 时间:2024/05/16 15:18

题目链接: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;}


0 0
原创粉丝点击