Programmer interview guide 8.2 typical recursive questions

Source: Internet
Author: User
Today, I read a book and saw 8.2 recursion problems. I tried the code on the book. I felt that there were still many bugs, so I wrote one myself. The main reason is that the Code in the book is only recursive for recursion, which is not good at recursive return processing. Paste your modified code for future review .. When writing a program, several problems are found. Note: 1. It must be v

Today, I read a book and saw 8.2 recursion problems. I tried the code on the book. I felt that there were still many bugs, so I wrote one myself. The main reason is that the Code in the book is only recursive for recursion, which is not good at recursive return processing. Paste your modified code for future review .. When writing a program, several problems are found. Note: 1. It must be v

Today, I read a book and saw 8.2 recursion problems. I tried the code on the book. I felt that there were still many bugs, so I wrote one myself.

The main reason is that the Code in the book is only recursive for recursion, which is not good at recursive return processing.

Paste your modified code for future review ..

Several problems are found during program writing. Note the following:

1. It must be a vector. ** Printarr = new vector * [Slen]; applies for pointer array initialization. during initialization, the value (* p + I) cannot be used as the left value. Use p [I] instead ..

2. problem Found: Use * (* p + I) = I; to assign a value. Only pointer + offset value can be used for calling. An error occurs when using arrays, for example, * p [I]. the array method can only be used to assign values. I still don't know whether it is a compiler problem. I will stay tuned.


# Include
# Include
# Include
# Include
Using namespace std;

// Create a vector that compares the position of string s in string p.
Void PrintfArrary (char * pstr, char * sstr, vector ** Printarr, int plen, int slen, int pstartnum, int sstartnum)
{


For (int I = sstartnum; I For (int j = pstartnum; j If (* (sstr + I) = * (pstr + j ))
(* Printarr [I]). push_back (j + 1 );
}

// Recursive call to build a sequence.
Void printseq (vector ** Printarr, int slen, vector * Out, int sum ){
If (slen = 0 ){
For (vector : Iterator I (* out). begin (); I! = (* Out). end (); I ++)
Cout <* I;
Cout < }


Else
{
Int I = sum-slen;
If (I ){
For (vector : Iterator j (* printarr [I]). begin (); j! = (* Printarr [I]). end (); j ++ ){
If (* j> * (* out). end ()-1 )){
(* Out). push_back (* j ));
Printseq (printarr, slen-1, out, sum );
(* Out). erase (* out). end ()-1, (* out). end ());
}
}
}
Else
For (vector : Iterator j (* printarr). begin (); j! = (* Printarr). end (); j ++ ){
(* Out). push_back (* j ));
Printseq (printarr, slen-1, out, sum );
(* Out). erase (* out). end ()-1, (* out). end ());
}
}
}

// Initialize and transmit data.
Void ConnectSequence (char * pstr, char * sstr)
{
Int plen = strlen (pstr );
Int slen = strlen (sstr );


Vector ** Printarr = new vector * [Slen];
For (int I = 0; I

Printarr [I] = new vector ;
}
Vector * Out = new vector ;
PrintfArrary (pstr, sstr, printarr, plen, slen, 0, 0 );
Printseq (printarr, slen, out, slen );
}


Int main (){
Char * a = "abdbccab ";
Char * B = "abc ";
ConnectSequence (a, B );
Return 0;
}

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.