Title: Rails
Topic Link: UVa514 link
Title Description:
There is a railway station in a city, and N carriages enter the station from a direction, numbered 1-n in the order of pit stops. Your task is to determine whether they can enter the B-direction rails in a particular order and drive into the station. For example, the stacking order (5 4 1 2 3) is not possible, but (5 4 3 2 1) is possible.
Topic Analysis:
In order to reorganize the car, with the transfer station, for each compartment, once from a to C can not go back to a, once moved from C to B, you can not go back to C, meaning is a->c and c->b. And in transit C, carriages conform to the principle of LIFO. So this can be seen as a stack.
Topic Analysis: It is still very important to feel the thought of this thing, a lot of things unexpectedly
The core of the code given in the book is the use of a variable to record the largest data that has been entered into the stack. If the data to be out of the stack is larger than this data, then the a++ will continue to loop. If it is smaller than this data, but it is not equal to the top of the stack, you can prove that this order is not possible.
#include <iostream>#include<stack>Const intmaxn= ++Ten;intTARGET[MAXN];using namespacestd;intMain () {stack<int>s; intn,t; T=1; target[1]=1; while(cin>>N) { while(cin>>target[1]&&target[1]){ for(intI=2; i<=n;i++) {cin>>Target[i]; } intA=0, b=1; intok=1; Stack<int>s; while(b<=N) { if(Target[b] = = a) {a++;b++;} Else if(!s.empty () &&s.top () ==target[b]) {S.pop (); b++;} Else if(a<=n) {S.push (a++);} Else{ok=0; Break; } } if(ok==1) cout<<"Yes"<<Endl; Elsecout<<"No"<<Endl; } cout<<Endl; } return 0;}
Do not know why still cannot ac, say is the expression error ... The heart is tired, asks the big God to instruct ...
Rails,uva 514