[Ox-off] and out of the stack sequence problem

Topics address

There are two clever algorithms Blue Book

Recursive. Consider a big problem into a small problem. Now we have to solve a length \ (N \) sequence of the last how many kinds of programs, referred to as \ (S_N \) , now assume that the sequence position \ (K \) where there is a number \ (A \) , \ (a \) preceded by \ (K-1 \) number to the stack, \ (a \) followed by \ (of NK \) number to the stack, the stack out of the total number of programs are \ (S_ {K -1} \) and \ (S_ {NK} \) so the big problem is transformed into a small problem, we will require a smaller \ (S_i \) , then there is recurrence formula (well understood):

\[S_N=\sum_{K=1}^NS_{K-1}*S_{N-K}\]

Dynamic Programming. We should have thought of this state and decision-making (this is really important, sometimes similar to the search). We set \ (F [i, j] \) is also \ (I \) elements not stack, \ (J \) elements in the stack of the total number of programs, the initial state is \ (F [0,0 ] = 1 \) , the target state is \ (F [N, 0] \) , every time we have a decision "to make a number of push," "let the top of the stack pop number," so the equation are:

\[F[i,j]=F[i-1,j+1]+F[i,j-1]\]

Guess you like

Origin www.cnblogs.com/BaseAI/p/11448962.html