[UVa-514] rails-stack learning, uva-514 rails

Source: Internet
Author: User

[UVa-514] rails-stack learning, uva-514 rails
UVa514 Rails (Rails)

Question: rails

Link: UVa514

Description:

There is A railway station in A city, with n trains entering the station from the direction, numbered 1-n in the order of arrival. your task is to determine whether you can allow them to enter the Track B in a specific order and enter the station. For example, the order of the output stack (5 4 1 2 3) is impossible, but (5 4 3 2 1) is possible.

Question Analysis:

In order to restructure the carriage, with the help of the transfer station, once every carriage is moved from A to C, it cannot return to A. Once it is moved from C to B, it cannot return to C, it means A-> C and C-> B. In the transfer station C, the carriage is in line with the principle of forward and backward. Therefore, it can be seen as a stack.

[Code]

 1 #include<cstdio> 2 #include<stack> 3 using namespace std; 4 const int N = 1005; 5 int n, tar[N], A, B; 6 int main() 7 { 8   while (scanf ("%d", &n), n) 9   {10     while (scanf ("%d", &tar[1]), tar[1])11     {12       for (int i = 2; i <= n; ++i)13         scanf ("%d", &tar[i]);14       stack<int> sta;15       A = B = 1;16       bool ok = true;17       while (B <= n)18       {19         if (A == tar[B])20         {   ++A; ++B;   }21         else if (!sta.empty() && sta.top() == tar[B])22         {   sta.pop();  ++B;  }23         else if (A <= n)24           sta.push (A++);25         else26         {   ok = false;  break;  }27       }28       printf (ok ? "Yes\n" : "No\n");29     }30     printf("\n");31   }32   return 0;33 }

 

[Analysis]

A Represents the first train currently waiting for entry in.

Tar [B] indicates the current departure train in the combat sequence

Stack sta stands for railway station (stack)

Judgment condition:

1. When A = tar [B], A immediately leaves the station, indicating that the current sequence can be implemented.

2. Compare the stack top (the train at the end of the station) with the input outbound sequence. If the sequence is the same, exit and continue the downward comparison.

3. If the above is not true, the current A will be pushed into the stack

4. the outbound sequence does not exist, that is, A> n. There are still trains in the station. This indicates that the input outbound sequence cannot be implemented.

 

[Summary]

Bool emply () determines whether the stack is empty

Void push () pushes new elements into the stack

Void pop () is used to bring up the top element of the stack when the stack is not empty.

Void top () is used to get the top element of the stack (but not delete it)

The stack definition of STL is in the header file <stack> and can be declared using "stack <int> s ".

 

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.