A simple application of LIFO stack

Source: Internet
Author: User

Description

There is a famous railway station and railway station in the city of Pushpop.

Each train is driven from a to the station, then from B to the station, and its carriages can be re-assembled in some form. Suppose the train coming from a direction has N carriages (n≤1000), numbered sequentially as 1,2,...,n. The staff responsible for the car's dispatch need to know if they can make it out in A1,a2,...,an order from B direction.
Please write a program to determine if you can get the specified compartment order. It is assumed that each compartment is not connected before entering the station, and that they can move themselves until they are on rails in the b direction. It is also assumed that the station can park any number of carriages. But once the train gets into the station, it can't go back to the A-direction track, and once it's in the B-direction rail, it can't get back to the station.

Input

Line 1th enters N, and the next line is any number of outbound reassembly sequences (A1,A2, ...,), with 0 as the end flag, separated by a space.

Output

The output yes indicates that the train (1,2,...,n) train can be arranged in the desired order (A1,A2, ...,), otherwise with no.

Sample Input

5
1 2 3 4 5 0

Sample Output

Yes

Just the simple application of the stack, A, a, a, a instead of two stations in and out of the train number, target[] Save the outbound order

1 //the stack is characterized by advanced post-2 /*1-n Station, can outbound in the given order3 */4#include <cstdio>5#include <iostream>6#include <stack>7 using namespacestd;8 Const intmax= ++Ten;9 intN;Ten intTarget[max]; One intMain () A { -      while(SCANF ("%d", &n) = =1) -     { thestack<int>s; -         intA=1; -         intb=1; -          for(intI=1; i<=n;i++) cin>>Target[i]; +         intok=1; -          while(b<=N) +         { A             if(A==target[b]) {a++;b++;} at             Else if(!s.empty () &&s.top () ==target[b]) {S.pop (); b++;} -             Else if(a<=n) S.push (a++); -             Else{ok=0; Break;} -         } -printf"%s\n"Ok?"Yes":"No"); -     } in     return 0; -}

A simple application of LIFO stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.