Number Algorithm of Popping Sequence in Data Structure

Number Algorithm of Popping Sequence in Data Structure

Recently, I encountered such a problem in the process of brushing data structure questions:


If a sequence of stacking sequence is e1, e2, e3, e4, e5, how many possible popping sequences exist? The final formula is simple, but in order to understand the principle, I want to write it here to help me use it later or help others.
Explanation of the principle:
a representative example of Cattelan number f(5)=42
The question of the number of stacking methods can be understood in this way:
Let the number of n numbers popping out of the stack be f(n)
because the order of stacking is determined, and the next mark For 1, 2, 3...n,
it is assumed that the last pop-up is the k-th number, which means that the k-1 numbers before k are pushed into the stack first, there are f(k-1) ways and then the nk numbers after
k There are f(nk) ways to push and pop the number from the stack
, and the last kth number is popped out of the stack. At this time, there are f(k-1)*f(nk) ways
and each number may be the last one. Stack
so
f(n)=f(0)*f(n-1)+f(1)*f(n-2)+…+f(k-1)*f(nk)+…+f (n-1)*f(0)
above f(0)=1 is to write a unified formula:
insert diagram here

The general term of the above formula is Cattelan number f(n)=C(n,2n)/n+1

The above is the principle of the explanation of the whole problem, which is based on the theoretical explanation of the predecessors. If anyone needs it, I hope it can help.

Supongo que te gusta

Origin blog.csdn.net/weixin_45717984/article/details/104721696
Recomendado
Clasificación