Recursive operation: start with the question of "reading program write running result ".

Source: Internet
Author: User

In this summer'sAlgorithmIn the exercise questions of the competition, there is such a "readProgramThe question of writing the running result (question 8th ),Test. c

 0001 # Include <stdio. h> 0002 # Deprecision max 100 0003 Void solve (char first [], int spos_f, int epos_f, char mid [], int spos_m, int epos_m) 0004 { 0005 Int I, root_m; 0006 If (spos_f> epos_f) 0007 Return; 0008 For (I = spos_m; I <= epos_m; I ++) 0009 If (first [spos_f] = mid [I])0010 { 0011 Root_m = I; 0012 Break; 0013 } 0014 Solve (first, spos_f + 1, spos_f + (root_m-spos_m), mid, spos_m, root_m-1 ); 0015 Solve (first, spos_f + (root_m-spos_m) + 1, epos_f, mid, root_m + 1, epos_m ); 0016 Printf ("% C", first [spos_f]); 0017 } 0018 Int main () 0019 { 0020 Char first [Max], mid [Max];0021 Int Len; 0022 Scanf ("% d", & Len ); 0023 Scanf ("% s", first ); 0024 Scanf ("% s", mid ); 0025 Solve (first, 0, len-1, mid, 0, len-1 ); 0026 Printf ("\ n "); 0027 Return 0; 0028 }

The call relationship of this function is as follows:

Note solve (...) Recursion.

When the input data is:

7

Abdcgf

Bdagecf

The output result is:

Dbgefca

How is this result produced? To better understand the running status of the program, I added some comments and debugging for the program.CodeAnd changed it to the following,Test1.c:

 0001 # Include <stdio. h> 0002 # Deprecision max 100 0003 Void solve (char first [], int spos_f, int epos_f, char mid [], int spos_m, int epos_m) 0004 { 0005 Int I, root_m; 0006  0007 Printf ("\ n execution function: Solve (first, % d, % d, mid, % d, % d); \ n", spos_f, epos_f, spos_m, epos_m ); 0008 Printf ("input first sequence: \ t "); 0009 For (I = spos_f; I <= epos_f; I = I + 1) printf ("% C", first [I]); 0010 Printf ("\ n passed-in mid sequence: \ t "); 0011 For (I = spos_m; I <= epos_m; I = I + 1) printf ("% C", mid [I]); 0012 Printf ("\ n "); 0013  0014 If (spos_f> epos_f) {/* If the input first interval does not contain any character, the function returns */ 0015 Printf ("this function has been executed: Solve (first, % d, % d, mid, % d, % d); \ n", spos_f, epos_f, spos_m, epos_m ); 0016 Return; 0017 } 0018 For (I = spos_m; I <= epos_m; I ++)/* from left to right, use root_m to mark the first character that is the same as that of first [spos_f] In mid */0019 If (first [spos_f] = mid [I]) 0020 { 0021 Root_m = I; 0022 Printf ("root_m = % d \ n", root_m ); 0023 Break; 0024 } 0025  0026 Printf ("Call the following functions: \ n "); 0027 Printf ("solve: % d \ t % d \ n", spos_f + 1, spos_f + (root_m-spos_m), spos_m, root_m-1 ); 0028 Printf ("solve: % d \ t % d \ n", spos_f + (root_m-spos_m) + 1, epos_f, root_m + 1, epos_m );0029  0030 Solve (first, spos_f + 1, spos_f + (root_m-spos_m), mid, spos_m, root_m-1 ); 0031 Solve (first, spos_f + (root_m-spos_m) + 1, epos_f, mid, root_m + 1, epos_m ); 0032 Printf ("% C", first [spos_f]);/* When the entire process ends, the start character of the first interval is printed */ 0033 Printf ("this function has been executed: Solve (first, % d, % d, mid, % d, % d); \ n", spos_f, epos_f, spos_m, epos_m ); 0034 } 0035 Int main () 0036 { 0037 Char first [Max], mid [Max]; 0038 Int Len;0039 Scanf ("% d", & Len ); 0040 Scanf ("% s", first ); 0041 Scanf ("% s", mid ); 0042 Solve (first, 0, len-1, mid, 0, len-1 ); 0043 Printf ("\ n "); 0044 Return 0; 0045 }

If we use the same input data, we will get more detailed results, which reflect solve (...) The specific running status of the function. The specific results are as follows:

Execution function: Solve (first, mid );

Input first sequence: abdcgf

Input mid sequence: bdagecf

Root_m = 2

Call the following functions:

Solve: 1 2 0 1

Solve: 3 6 3 6

Execution function: Solve (first, 1, 2, mid, 0, 1 );

Input first sequence: BD

Input mid sequence: BD

Root_m = 0

Call the following functions:

Solve: 2 1 0-1

Solve: 2 2 1 1

Execute the function: Solve (first, mid, 0,-1 );

Input first sequence:
The input mid sequence:
This function has been executed: Solve (first, mid, 0,-1 );

Execution function: Solve (first, mid );

Input first sequence: d

Input mid sequence: d

Root_m = 1

Call the following functions:

Solve: 3 2 1 0

Solve: 3 2 2 1

Execution function: Solve (first, mid );

Input first sequence:
The input mid sequence:
This function has been executed: Solve (first, mid );

Execution function: Solve (first, mid );

Input first sequence:
The input mid sequence:
This function has been executed: Solve (first, mid );

D. The function is executed successfully: Solve (first, mid );

B. The function is executed successfully: Solve (first, mid );

Execution function: Solve (first, mid );

Input first sequence: cepidermal

Input mid sequence: gecf

Root_m = 5

Call the following functions:

Solve: 4 5 3 4

Solve: 6 6 6 6

Execution function: Solve (first, mid );

Input first sequence: eg

Input mid sequence: GE

Root_m = 4

Call the following functions:

Solve: 5 5 3 3

Solve: 6 5 5 4

Execution function: Solve (first, 5, 5, mid, 3, 3 );

Input first sequence: G

Input mid sequence: G

Root_m = 3

Call the following functions:

Solve: 6 5 3 2

Solve: 6 5 4 3

Execute the function: Solve (first, 6, 5, mid, 3, 2 );

Input first sequence:
The input mid sequence:
This function has been executed: Solve (first, mid );

Execution function: Solve (first, 6, 5, mid, 4, 3 );

Input first sequence:
The input mid sequence:
This function has been executed: Solve (first, mid );

G. The function is executed successfully: Solve (first, 5, mid );

Execution function: Solve (first, 6, 5, mid, 5, 4 );

Input first sequence:
The input mid sequence:
This function has been executed: Solve (first, mid );

E. The function is executed successfully: Solve (first, 5, mid );

Execution function: Solve (first, 6, 6, mid, 6, 6 );

Input first sequence: F

Input mid sequence: F

Root_m = 6

Call the following functions:

Solve: 7 6 6 5

Solve: 7 6 7 6

Execution function: Solve (first, 7,6, mid, 6,5 );

Input first sequence:
The input mid sequence:
This function has been executed: Solve (first, mid );

Execution function: Solve (first, 7,6, mid, 7,6 );

Input first sequence:
The input mid sequence:
This function has been executed: Solve (first, mid );

F: Solve (first, 6, 6, mid, 6, 6 );

C. The function is executed successfully: Solve (first, mid );

A: Solve (first, mid );

We will comment on test1.c.Please pay attention to the following points:

1. In Solove (...) Data in first [] and mid [] will not be changed. Only solve (…) is changed (...) The four parameters passed in. Among them, spos_f refers to the start position of first, and epos_f refers to the end position of first. Similarly, spos_m refers to the start position of mid, epos_m refers to the end position of the MID interval.

2. In Solove (...) The input parameters of recursive calls are constantly changing. Solove (...) Root_m is also used. This computation process is not complex, but it makes me very puzzled:What is the purpose of this recursive call and calculation?Sort? Search? I really don't understand. Although I do not understand the meaning of the entire program, this does not affect the calculation of the program running results. During the competition, a table similar to the following can be listed to calculate the running result of the program:

First line
Spos_f

Epos_f

Spos_m
Epos_m
Root_m
Row 2

0

6

0

6

2

Row 3
Spos_f + 1
Spos_f + (root_m-spos_m)
Spos_m
Root_m-1
 
Row 4
Spos_f + (root_m-spos_m) + 1
Epos_f
Root_m + 1
Epos_m
 
The first line is the five key variables that control the program running. The second row is the current value of these five variables. Line 3 and Line 4 are Solove (...) 2 Solove (…) derived from recursive calls (...) Function parameters. The solve (…) derived from the third row (...) Is the solve (…) to be prior to the fourth line (...) Executed. Solove (…) corresponding to Lines 3 and 4 (...), During the execution, calculate the respective root_m, which is 18 ~ From test1.c ~ Calculated by 24 rows.

3. Print data output only when the last sentence before the function returns is 32nd rows. The output data is the leftmost character in the interval of the first [] of the function to be returned. Therefore, we have two concerns:

① When a function is called, what is the leftmost character subscript (spos_f?

② When will the function be returned?

Another problem that troubles students is: 1 Solove (...) Two Solove (…) are derived (...), This is the so-called recursion. What is their execution order?

You can see the answer to this question by carefully reading the running result of test1.c. The call relationship between them is shown below :(The black arrow indicates the call, and the red arrow indicates the return)

Some people may think that how does a computer implement this call and return process?

The computer uses a data structure called "stack", which is a one-dimensional linear data structure that can complete the call process in the preceding figure.

STACK: http://zh.wikipedia.org/wiki/%E5%A0%86%E6%A0%88

Http://en.wikipedia.org/wiki/Stack_%28data_structure%29

The details are as follows:

Note the following when reading:

1. At the top of the stack, all functions are being executed.

2. Upper-layer functions are called (derived) by lower-level functions. The upper layer is the son of the lower layer. This is very important. You need to understand it carefully when looking at the image.

3. Through these call processes, we can use stacks to organize the tree-like call/return structure.

4. In the stack, the function at the top of the stack calls new functions and generates new stacks. In the stack, the function at the top of the stack is executed (extinct ), the stack is moved down.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Oh, the final result is:

Dbgefca

Note: This question is very confusing. Almost every algorithm competition has such a question of "reading program writing running results", which is unknown to me. Who knows the purpose or purpose of this code? Or is the purpose of an algorithm competition only to speed the hands-on computing of contestants?

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.