Two-way recursive algorithm problem

Source: Internet
Author: User

First question: Give the stacking order of {1, 2, 3,..., n}, and output all possible stacking order

#include"stdafx.h"#include<stack>#include<queue>#include<iostream>#include<cstdio>#include<cstdlib>using namespacestd;intn =0; typedef stack<int>stack;typedef Queue<int>Queue;voidDfsintLevel,stack s, Queue buf,Const intLevel );/** Each recursive has only one element (level) in the stack, but there may be 0 elements out of the stack, there may be at least one element out of the stack * @param level present scale the size of current Sub-problem * @param s current sub-problem stack the stack of current Sub-problem * @param buf the print queue of the present sub-issue the printing queue of the Sub-p Roblem * @param level largest The maximum scale of the problem*/voidDfsintLevel,stack s, Queue buf,Const intLevel ) {    //by using ' #pragma region xxx ' direction, the code can appear more gracefully~#pragmaRegion termination of recursionif(Level = =Level ) {        //First We print all elements in the queueBuf.push (level);  while(!Buf.empty ()) {cout<<buf.front () <<" ";        Buf.pop (); }        //Then we clear the stack         while(!S.empty ()) {cout<< s.top () <<" ";        S.pop (); } cout<<Endl; N++; return; }#pragmaEndregion, when Level==level, the recursion stops.Queue Tempqueue (BUF);    S.push (level);    Stack Tempstack (s); /** Very Important steps * Break the current problem into several sub-problems * must take into account all possible sub-problems, such as * 1. Push then no pop * 2. Push then pop 1 * 3. Push then pop 2 * ...*/     while(!S.empty ())        {Buf.push (S.top ());        S.pop (); DFS ( level+1, S, buf, level); } DFS ( level+1, Tempstack, tempqueue, level);}int_tmain (intARGC, _tchar*argv[]) {unsignedintcases; intx; scanf ("%d", &cases);  while(cases--) {Stack s=Stack (); Queue Q=Queue (); CIN>>x; DFS (1, S, q, x); cout<<"The Catalan number of"<<x<<" is"<<n<<Endl; N=0; } System ("shutdown-s-T");}

Two.

Input a positive integer n (n>=3), write a function to print all the possibilities of combination of 1,1,2,2,3,3,......, n , n (2n numbers altogether) that satisfies:
There is 1 integer between "1" s
There is 2 integers between "2" s
There is 3 integers between "3" s
......
There is n integers between "n" s
For example of 3,the, possible sequence are:231213 or 312132.
Pay attention, there is 0 or many possible sequences, print them all

Result class Result.h

#pragmaOnceclassresult{Private:    int*data; intN; Public: Result (int_n): N (_n) {data=New int[2*N];    Recover (); }    ~Result () {delete[] data;} Result (Constresult&src) {N=SRC.N; Data=New int[2*N];  for(intI=0; i<2*n; ++i) {Data[i]=Src[i]; }    }    int&operator[](intindex) {        returnData[index]; }    Const int&operator[] (intIndexConst    {        returnData[index]; }    BOOLInsertintIintLevel ) {        if(i<0|| i+level+1>2*n)return false; if(data[i]==0&& data[i+level+1]==0) {Data[i]= data[i+level+1] =Level ; return true; }        Else return false; }    voidrecover () { for(intI=0; i<2*n; ++i) {Data[i]=0; }} Result&operator=(Constresult&src)        {delete[] data; N=SRC.N; Data=New int[2*N];  for(intI=0; i<2*n; ++i) {Data[i]=Src[i]; }        return* This; }    intSize ()Const    {        return 2*N; }    voidPrint ()Const    {         for(intI=0; i<2*n; ++i) printf_s ("%d", Data[i]); printf_s ("\ n"); }};

Core algorithms:

#include"stdafx.h"#include"Result.h"#include<stack>#include<queue>#include<list>#include<vector>using namespacestd;voidDfsintLevel , Result Res) {    if(level<1) {res.print (); return; } Result Temp=Res;  for(intI=0; I<res.size ()-level-1; ++i) {if(Temp.insert (I, level)) {DFS ( level-1, temp); Temp=Res; }        Else Continue; }}int_tmain (intARGC, _tchar*argv) {    intcases; scanf_s ("%d", &cases);  while(cases--)    {        intN; scanf_s ("%d", &N); Result Res=Result (n);    DFS (n, res); }}

Two-way recursive algorithm problem

Related Article

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.