Code for implementing the C ++ train entry Algorithm
This article mainly introduces the implementation code of the C ++ train entry algorithm. For more information, see
[Problem description]
There is a railway station in a city where rails are laid. There are n trains entering the station from the direction, numbered 1 ~ N. Your task is to let them enter the Track B in a specific order and drive out of the station. To restructure the carriage, you can use Transfer Station C. This is a station that can park any number of trains, but because of the end cap, the carriage that enters C must be driven out in reverse order. For each carriage, once it is moved from A to C, it cannot return to A. Once it is moved from C to B, it cannot return to C. In other words, there are only two options at any time: A → C and C → B.
This problem is similar to the train entry in the previous data structure experiment and is simpler. I tried to write it myself, but there is still a big gap between the amount of code for the reference answers in the book. The Code is as follows:
The 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;
Cin> B [I]; // The Order of the output stack.
}
Int top =-1;
Int count = 0;
Int I = 0;
For (;;)
{
If (I <n)
{
++ Top;
Stack [top] = a [I ++]; // inbound stack
Cout <"PUSH" <endl;
}
If (stack [top] = B [count])
{
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:
The Code is as follows:
# 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; // A is used to record the number of incoming stacks, and B is used to record the train number of the derailed train
For (int I = 1; I <= n; I ++)
Cin> target [I]; // record the derailment Sequence
Int OK = 1;
While (B <= n)
{
If (A = target [B]) {A ++; B ++ ;}
Else if (top & stack [top] = target [B]) {top --; B ++;} // output stack
Else if (A <= n) stack [++ top] = A ++; // inbound stack
Else {OK = 0; break ;}
}
If (OK)
Cout <"Yes" <endl;
Else
Cout <"No" <endl;
}
Similarly, STL can be used for implementation. You only need to make minor changes to the answers referenced in the book. The Code is as follows:
The Code is as follows:
/* Implement the STL stack */
# Include <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)
{
If (A = target [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]
There is still room for optimization for self-written code. Learn the clear logic expression of the reference answer. Learn how to use the STL stack.
In the data structure experiment, you should add a judgment on the restrictions on the train's inbound rail.
Beginners of C ++, who do not know how deep they are, are currently taking a step by step in the "water question" phase, and crossing the river by feeling the stones and aligning them with Daniel.