Train into stack out stack sequence

Source: Internet
Author: User
A stack may is regarded as a railway switching network like the one in the figure.
Cars numbered 1, 2, ..., n are on the "on the", and it is desired to rearrange (permute) the cars as they leave on t He right-hand track. A car "is" on the spur (stack) can are left there or sent on their way down the right track, but it can never being sent back to the incoming track. For example, if n=3, and we have the "Cars 1,2,3 on" the left track, then 3-A-goes. We could then send 2 to the spur, then on its way to the right, then send 3 on the way, then 1, obtaining the New Order 1, 3,2.
For the general N, find how many permutations can is obtained by the using this stack. and output all of the permutations by using the switching network in the figure.

Analysis

Three tracks corresponding to left, down, right ... It is obvious that using recursive method to solve this problem is very intuitive and easy to understand

To simplify the analysis, assume that the number of left is down, (even if it goes straight to right as if it were first to down and then right), and the number cannot be returned from down or to the left.

There are 3 cases where the number is down and right when there is no number in the left, in which case the order is determined and the order cannot be changed. The right number in turn out of the stack output, and then the number of down to the stack output when there is no number in the down, this time can only be left in the number of stacks, pressure to down. When it is not in both of these situations, that is, left and down,

There are two choices at this time.

1.) put a number of left in the stack, pressure down in

2.) The number in left is unchanged, the number of down in the stack, pressure to right

Obviously, left, which operates at one end, can be designed as stack;

Similarly, down is only operated at one end and has advanced first out features that are suitable for the design of stack

And right needs to operate at both ends, and in the 1th case, you need to output the stack at one end, and in the 3rd case, the number from down will be pressed to the other end of the right, so you can design the right as a two-terminal queue deque

By the above analysis, with the help of C + + STL template class, it is easy to write out the specific implementation, code see file Train.cpp

All arrange output to file D:/output.txt


Because the total number of permutations of this problem is Catlan number, the formula is calculated by the Catlan number, and a function is written to verify the correct number of solutions to the recursive method.

On the number of Catalan, this article is visible

The following are all permutations of the n=4, and the output sequence is left to right
4 3 2 1 (4 first out stack, 1 final stack)
4 3 1 2
4 2 3 1
4 2 1 3
4 1 2 3
3 4 2 1
3 4 1 2
3 2 4 1
3 2 1 4
3 1 2 4
2 3 4 1
2 3 1 4
2 1 3 4
1 2 3 4
Total number of ' all ' permutations is 14


Train.cpp

#include < iostream >
#include < FStream >
#include < stack >
#include < queue >

using namespace Std;


int counter = 0;

Long double Catalan (int n)
... {
if (n==0) return 1;
else return (4*n-2) *catalan (n-1)/(n+1);
}


void recur (Stack < int > left, stack < int > down, deque < int > right, Ofstream & Fout)
... {
int left_num=left.size ();
int down_num=down.size ();
if (left_num==0)
... {
while (!right.empty ())
... {
Cout<<right.front () << "";
Fout<<right.front () << "";
Right.pop_front ();
}
while (!down.empty ()) ... {
Cout<<down.top () << "";
Fout<<down.top () << "";
Down.pop ();
}
fout<<endl;
cout<<endl;
counter++;
return;
}
else if (down_num==0)
... {
Down.push (Left.top ());
Left.pop ();
Recur (left, down, right, fout);
}
else ... {

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.