【栈】 poj 1363

时间:2023-03-09 03:57:36
【栈】 poj 1363

poj1363,这道题是数据结构与算法中的经典问题,给定一组进栈顺序,问栈有多少种出去的顺序。

#include<stdio.h>
#include <stack>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
//    freopen("in.txt","r",stdin);
    int n;
    while(scanf("%d",&n)&&n)
    {
        stack<int> sta;
        ],i;
        ]))
        {
            while(!sta.empty())   sta.pop();
            ]==)
            {
                putchar('\n');break;}
                ; i<n; i++)
                    scanf("%d", &s[i]);
                ;
                ;i<=n;)
                {
                    while(sta.empty()||(!sta.empty()&&sta.top()<s[j]))
                    {
                        sta.push(i++);
                    }
                    while(!sta.empty() && sta.top()==s[j])
                    {
                            j++;sta.pop();
                    }
                    if(!sta.empty() && j<n && sta.top()>s[j])break;
                }
                if(sta.empty()==true) cout <<"Yes" << endl;
                else cout << "No" << endl;
            }
        }
    ;
}