UVA 514 Rails(栈)

来源:互联网 发布:php如何检测usbkey 编辑:程序博客网 时间:2024/05/16 13:45

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=455

题       意:给你一个组数,问将1到n一次入栈后,能否按此顺序输出。

思       路:定义一个栈来维护就行了。

代码如下:

#include <iostream>using namespace std;#include <string.h>#include <stdio.h>#include <stack>#include <algorithm>int vis[1200];int main(){    int n;    while( scanf ( "%d", &n ) != EOF )    {        stack<int>st;        if( n == 0 ) break;        while( 1 )        {            for( int i = 1; i <= n; i ++ )            {                scanf ( "%d", &vis[i] );                if(vis[1]==0) break;            }            if( vis[1] == 0 ) break;            int a = 1, b = 1, ans = 1;            while( b <= n )            {                if( a == vis[b] )                {                    a++;                    b++;                }                else if( !st.empty() && st.top() == vis[b] )                {                    st.pop();                    b++;                }                else if( a <= n ) st.push(a++);                else                {                    ans=0;                    break;                }            }            if( ans == 1 ) printf("Yes\n");            else printf("No\n");        }        printf("\n");    }    return 0;}


 

0 0
原创粉丝点击