且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

uva 514 Rails

更新时间:2022-08-19 22:48:16

点击打开链接uva 514

思路: 栈模拟
分析:
1 题目给定一个不变的入栈的顺序1~n,然后再给出一个序列问是否是一个满足条件的出栈顺序
2 每一个case后面输出空行,直接去模拟栈即可

代码:

#include<stack>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 1010;

int n , num[MAXN];
stack<int>stk;

bool isOk(){
    while(!stk.empty())
        stk.pop();
    int i , j;
    i = j = 1;
    while(j <= n){
         if(i < num[j]){
             stk.push(i++);
         }
         else if(i == num[j]){
             i++;
             j++;
         }
         else if(!stk.empty()){
             if(stk.top() != num[j])
                 return false;
             stk.pop();
             j++;
         }
    }
    return true;
}

int main(){
    int x;
    bool first = true;
    while(scanf("%d" , &n) && n){
         while(scanf("%d" , &num[1]) && num[1]){
              for(int i = 2 ; i <= n ; i++) 
                  scanf("%d" , &num[i]); 
              printf("%s\n" , isOk() ? "Yes" : "No");
         } 
         puts("");
    } 
    return 0;
}