Stack a given push sequence to determine whether the given sequence is a pop sequence ~ ! Output! ! Output! ! Output! In and out! Exhausted me ......

Source: Internet
Author: User

Question:

Enter Two integer sequences. One sequence represents the push sequence of the stack and determines whether the other sequence may be in the pop sequence. For the sake of simplicity, we assume that any two integers in the push sequence are not equal.

For example, if the input push sequence is 1, 2, 3, 4, 5, 3, 2, and 1, it may be a pop series. It can have the following push and pop sequences: Push 1, Push 2, Push 3, Push 4, Pop, Push 5, pop, the pop sequence is 4, 5, 3, 2, and 1. However, sequences 4, 3, 5, 1, and 2 cannot be pop sequences of push sequences 1, 2, 3, 4, and 5.

 

Solution:

When you don't know how to do it, let's first look at how smart people think. For example, in the above example, the push sequence is called a, the pop sequence is called B, and the position of the first number 4 of B in a is found, I found it all at once (here I am deeply despising computers without our good eyesight). 4 in B, the first one indicates that first 1, 2, 3, and 4 should be pushed to the stack, and then 4 should be pushed out of the stack, in this way, 4 is the first one. Then, let's look at the second number 5 of B and use the sharp eyes of humans to find its position in a. It's Okay To find it behind 4. This shows that we continue to push 5 into the stack and then go out of the stack. Imagine what if B is not 5 but 3? It is also possible, as long as the top of the stack is out. But suppose this is 1 for B? That's not enough, because 1 is already at the bottom of the stack, not at the top of the stack.

From the above analysis, we can see that we only need to determine the position of the first number of B series in a, from which a is divided into three parts, the preceding example is divided into three parts: 123, 4, and 5. A cursor is defined to point to the end of the first part (for example, if PST is defined, it points to 3 of the First Part ), define a cursor pointing to the first of the third block (for example, if Prest is defined, it points to 5 of the third block ). Then, starting from the second number of B, check whether it is equal to PST. If it is equal, it means that this number can be obtained through the out stack. Then this number appears at this position; what if it doesn't wait? It depends on whether it is equal to the prest. If it is equal, it indicates that this number can be obtained by going into the stack and then going out of the stack. Then there is no problem with this number in this position. If it is different from PST and Prest, this number is generated in the wrong position.

In fact, it is spread from the center to the left and right sides to see if it is equal to the number of boundaries. This is the case. Code:

1 # include <iostream> 2 # include <string> 3 4 Using STD: string; 5 Using STD: CIN; 6 Using STD: cout; 7 Using STD :: endl; 8 9 bool HS (const string & strsrc, const string & strjudge) 10 {11 // initialize 12 INT PPOs = strsrc based on the first letter of the string to be compared. find (strjudge [0]); 13 int PST =-1, Prest = strsrc. length (); // two cursors 14 if (PPOs> 0) 15 PST = PPOs-1; 16 if (PPOs <strsrc. length ()-1) 17 Prest = PPOs + 1; 18 19 for (int ii = 1; II <strsrc. length ()-1; II ++) // The Last One does not need to be managed for 20 {21 if (PST> = 0 & strjudge [II] = strsrc [PST]) // same as 22 PST --; 23 else if (Prest <= strsrc. length ()-1 & strjudge [II] = strsrc [Prest]) // 24 Prest ++; 25 else26 return false; 27} 28 return true; 29} 30 31 int main (void) 32 {33 string strsrc ("ABCDE"); 34 cout <HS (strsrc, "dceab "); 35 cin. get (); 36}

Stack a given push sequence to determine whether the given sequence is a pop sequence ~ ! Output! ! Output! ! Output! In and out! Exhausted me ......

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.