UVA514Rails

来源:互联网 发布:红色报刊档案数据库 编辑:程序博客网 时间:2024/06/11 03:20
//UVA514#include<cstdio>#include<cstdlib>#include<stack>using namespace std;const int MAXN = 1000 + 10; int main() {int n = 0, tmp;int count = 0;while(scanf("%d", &n) && n) {if(count++) printf("\n");while(1) {        int out[MAXN] = {0};        int in = 1;        scanf("%d", &out[1]);        if(out[1] == 0) break;    for(int i = 2; i <= n; i++) scanf("%d", &out[i]);    int index = 1;    stack<int> s;    int ok = 1;    while(index <= n) {    if(out[index] == in) {    index++; in++;//在之前的输入输出数据合法性已经检验完毕时,当前的输入数据和输出数据相等,说明单个输入值入栈后直接出栈了     }    else if(!s.empty() && s.top() == out[index]) {     s.pop(); index++;//栈中的数据该出列的了     }    else if(in <= n) {        s.push(in++);//当前输入数据还没有到直接出去的时候,那么除了不合法外,只能是应先入栈         }        else {        ok = 0; break;        }    //printf("index = %d, in = %d*\n", index, in);        }        if(ok) printf("Yes\n");        else printf("No\n");         //scanf("%d", &n);        /*if(!n) {        printf("\n");        break;    }*/    }}printf("\n");return 0;} /*51 2 3 4 55 4 1 2 3066 5 4 3 2 100*/

原创粉丝点击