Algorithm competition entry classic 6.1.2 stack and queue-rails, algorithm competition entry classic rails

Source: Internet
Author: User
There is a railway station in a city, and the tracks are laid as shown below. There are n cars entering the station from the direction A, and they are numbered 1 ~ n in the order of arrival. Your task is to get them into the tracks in the B direction and get out of the station in a certain order. In order to reorganize the cars, you can use the transit station C; this is a station that can park any number of cars, but because the end is capped, the cars entering C must exit C in the reverse order. For each compartment, once you enter C from A, you cannot return to A; once you enter B from C, you cannot return to C. In other words, at any time, there are only two options: A-> C and C-> B.
Sample input:
5
1 2 3 4 5
5
5 4 1 2 3
6
6 5 4 3 2 1
Sample output:
Yes
No
Yes


 1 #include <stdio.h>
 2 #define MAXN 1000 + 10
 3 int n, target [MAXN];
 4
 5 int main (void)
 6 {
 7 while (scanf ("% d", & n) == 1)
 8     {
 9 int stack [MAXN], top = 0;
10 int A = 1, B = 1;
11 for (int i = 1; i <= n; i ++)
12 scanf ("% d", & target [i]);
13 int ok = 1;
14 while (B <= n)
15 {
16 if (A == target [B]) {A ++; B ++;} // The car enters and leaves the transit station C in order, then it exits the loop
17 else if (top && stack [top] == target [B]) {top--; B ++;} /// If the car enters the transit station C in reverse order, it will jump out of the loop
18 else if (A <= n) stack [++ top] = A ++; // Adjust the carriages to reverse transfer station C
19 else {ok = 0; break;} // the carriages are neither entering nor leaving the transit station C in order or in reverse order
20}
21 printf ("% s \ n", ok? "Yes": "No");
twenty two     }
23 return 0;
twenty four }
View Code
analysis:
栈 1. Stack: In the transit station C, the carriage conforms to the principle of last-in, first-out, called the stack, which is the LIFO table; where LIFO stands for Last In First Out. Since only one end of the stack grows, you only need an array stack and a pointer to the top of the stack (which always points to the top element of the stack).
2. For the second input case:
B <= n A <= n stack [++ top] = A ++;
1 <= 5 1 <= 5 stack [1] = 1; top = 1; A = 2;
           2 <= 5 stack [2] = 2; top = 2; A = 3;
            3 <= 5 stack [3] = 3; top = 3; A = 4;
            4 <= 5 stack [4] = 4; top = 4; A = 5;
            5 <= 5 stack [5] = 5; top = 5; A = 6;
Top && stack [top] == target [B] top--; B ++;
5 && 5 == 5 top = 4; B = 2;
4 && 4 == 4 top = 3; B = 3;
3 && 3 == 1 Out of the while loop
3. For convenience, the array index starts from 1. For example: target [1] refers to the number of the first car in the target sequence, and stack [1] refers to the bottom element of the stack (the stack is empty if and only if top = 0).

C ++ provides a simpler way of processing-STL queue:


 1 #include <cstdio>
 2 #include <stack>
 3 using namespace std;
 4 #define MAXN 1000 + 10
 5 int n, target [MAXN];
 6
 7 int main (void)
 8 {
 9 while (scanf ("% d", & n) == 1)
10 {
11 stack <int> s;
12 int A = 1, B = 1;
13 for (int i = 1; i <= n; i ++)
14 scanf ("% d", & target [i]);
15 int ok = 1;
16 while (B <= n)
17 {
18 if (A == target [B]) {A ++; B ++;} // The car enters and leaves the transit station C in order, then the loop is out
19 else if (! S.empty () && s.top () == target [B]) {s.pop (); B ++;} // If the car enters the transit station C in reverse order, it will jump out of the loop
20 else if (A <= n) s.push (A ++); // Adjust the carriage to reverse transfer station C
21 else {ok = 0; break;} // The carriages are neither entering nor leaving the transit station C in order or in reverse order
twenty two         }
23 printf ("% s \ n", ok? "Yes": "No");
twenty four     }
25 return 0;
26}
View Code
Reprinted at: https://www.cnblogs.com/zhuangwei/p/5352781.html
Related Article

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.