This article mainly introduces the C + + train into the orbit algorithm implementation code, the need for friends can refer to the
"Problem description" A city has a railway station, and the rails are laid out as shown in the picture. There are n carriages from a direction into the station, according to the entry sequence number for 1~n. Your task is to have them enter the B-direction rails in a certain order and drive out of the station. In order to reorganize the car, you can use the transit point C. This is a station that can park any number of carriages, but because of the end cap, the carriages entering C must be pulled out in the reverse order. For each compartment, once you move from a to C, you can't go back to a; Once you move from C to B, you can't go back to C. In other words, at any given moment, there are only two choices: A→c and C→b. The problem is similar to the one in the previous data structure experiment, and is simpler. Try to write yourself, and the book reference to the number of the answer to the amount of code still a big gap. The code is as follows: code is as follows: #include <iostream> using namespace std; const int maxsize=100; void Main () { int n; cin>>n; int a[maxsize],b[maxsize]; INT Stack[maxsize]; for (int i=0;i<n;i++) { a[i]=i+1; CI n>>b[i]; //out Stack order } int top=-1; int count=0; int i=0; for (;;) { if (i<n) { & nbsp ++top;   stack[top]=a[i++]; //into stack cout<< "PUSH" <<endl; } if (Stack[top]==b[count]) {&N Bsp top--;count++; cout<< "POP" <<endl; } ELSE if (i==n) { cout<< "NO" <<endl; break; } if (count==n) { cout<< "YES" <<endl; break; } if (top<-1) {   cout<< "NO" <<endl; break; } { } The reference code is as follows: Code is as follows: &NB SP; #include <iostream> using namespace std; const int MAXN=1000+10; int N,TARGET[MAXN]; void Main () { while (cin>>n) { int stack[maxn],top=0; int a=1,b=1; &NB Sp //a used to record the number of stacks, B used to record the derailment of the train number for (int i=1;i<=n;i++) & nbsp cin>>target[i]; &NB Sp //record derailment sequence int ok=1; while (b<=n) &NBSP { if (A==target[b]) {a++; b++;} ELSE if (top && stack[top]==target[b]) {top--; b++;} //out stack ELSE if ((a<=n)) stack[++top]=a++; //into stack else {ok= 0;break;} } if (OK) cout<< "Ye S "<<endl; else cout<< "No" <<endl; } Similarly, you can use STL to achieve, just the book reference to the answer to make minor changes, the code is as follows: Code as follows: &NBSP;/*STL stack to achieve * * * #inclu De<iostream> #include <stack> using namespace std; const int MAXN=1000+10; int N,TARGET[MAXN]; int main () { while (cin>>n) { stack<int> s; int a=1,b=1; for (int i=1;i<=n;i++) cin>>target[i]; int ok=1; while (b<=n) { A==T Arget[b]) {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) cout<< "YES" <<endl; else cout<< "NO" <<endl; } "Summary" write your own code there is still room for optimization. Learn the clear and logical expression of a reference answer. Learn the use of STL stacks. Contact data structure in the experiment on the train into the rail upgrade, the limit on the buffer rail, you should add a judgment can be. Do not know how deep the pit of C + + beginners currently stay in the "water problem" stage to take a step to see Stones to Daniel in line with &nbsP