Analysis of exercises for software C/C ++ development, and software exercises

Source: Internet
Author: User

Analysis of exercises for software C/C ++ development, and software exercises

Analysis of exercise questions in software C/C ++ development.

Code Filling Area

Code blank question:The player is required to fill in the missing part based on understanding the working principle of the given Code, so that the program logic is correct and complete.

You can directly submit the code (only the answers in the blanks, excluding the existing code or symbols on the question surface) on the web page. Do not write unnecessary content.

Use the ansi c/ansi c ++ standard and do not rely on special functions provided by the operating system or compiler.

This part of the question is often the simplest (so the relative score is relatively small), which is what we should strive to do right.

Because the question will provide the main code, you can often run and modify the code to check whether the missing part is correct.

Question 5 Title: letter group string
The three letters A, B, and C can form many strings.
For example: "A", "AB", "ABC", "ABA", "AACBB "....
Now James is thinking about the following question:
If the number of each letter is limited, how many strings of known length can be formed?
He asked his friends to help and soon got the code,
The solution is super simple, but the most important part is not detailed.
Analyze the source code carefully and enter the content that is missing in the dash section.
# Include
 
  
// A. A. B. B. c. The number of different strings with n length. Int f (int a, int B, int c, int n) {if (a <0 | B <0 | c <0) return 0; if (n = 0) return 1; return ______________________________________; // fill in the blanks} int main () {printf ("% d \ n", f )); printf ("% d \ n", f (1, 2, 3); return 0 ;}
 
For the above test data, James's oral calculation result should be:
6
19

Note: enter only the code that is missing from the dashes. do not submit any additional content or descriptive text.

Analysis

The writing is very short. Through comments, we can see that f (a, B, c, n) can directly find the answer.

Then we need to consider how f (a, B, c, n) is calculated.

First, it considers the situations where a, B, and c are less than 0 or n = 0. This is a special case where the solution can be obtained directly, so the rest of the cases cannot be solved directly.

Then we need to convert it into several subproblems until it is converted into several special cases.

From the known beginning, a, B, and c are less than 0. These indicate invalid states.

If n = 0, the answer is 1. What about n = 1?

N = 1 is equivalent to adding a letter after n = 0. That is to say, I have several letters and I have several possibilities.

In the case of n = 1, to convert it to n = 0, I only need to subtract one of the abc3 letters. Of course, you can also consider this if there are no letters, because the Code has already written illegal information for us.

Generally, we can launch f (a, B, c, n) = f (A-1, B, c, n-1) + f (a, B-1, c, n-1) + f (a, B, C-1, n-1)

Then the statement should be filled in f (A-1, B, c, n-1) + f (a, B-1, c, n-1) + f (a, B, C-1, n-1)

You will find that function f contains the use of several f functions. This method is called recursion.

Recursion consists of recursive functions and recursive exit. The special case given in the question is the recursive exit, and we implement the recursive function.

Code and running result
# Include
 
  
// A. A. B. B. c. The number of different strings with n length. Int f (int a, int B, int c, int n) {if (a <0 | B <0 | c <0) return 0; if (n = 0) return 1; return f (A-1, B, c, n-1) + f (a, B-1, c, n-1) + f (a, B, c-1, n-1); // fill in the blanks} int main () {printf ("% d \ n", f (,); printf ("% d \ n ", f (1, 2, 3); return 0 ;}
 

 

Question 6

Title: maximum public substrings
The maximum length of Public substrings is:
Evaluate the maximum length that can be matched among all substrings of two strings.
For example, "abcdkkk" and "baabcdadabc ",
The longest common substring that can be found is "abcd", so the maximum length of the common substring is 4.
The following program uses the matrix method for solving the problem. This is a relatively effective solution for the case where the string size is not large.
Analyze the solution ideas and complete the missing code.

# Include
 
  
# Include
  
   
# Define N 256int f (const char * s1, const char * s2) {int a [N] [N]; int len1 = strlen (s1 ); int len2 = strlen (s2); int I, j; memset (a, 0, sizeof (int) * N); int max = 0; for (I = 1; I <= len1; I ++) {for (j = 1; j <= len2; j ++) {if (s1 [I-1] = s2 [J-1]) {a [I] [j] = __________________________; // fill in the blank if (a [I] [j]> max) max = a [I] [j] ;}} return max;} int main () {printf ("% d \ n", f ("abcdkkk", "baabcdadabc"); return 0 ;}
  
 
Note: Only the missing code is submitted. do not submit the existing code and symbols. Do not submit descriptive text.
Analysis

Observe the code. The answer is "max", which represents the maximum value in a two-dimensional array.

And a [I] [j] will be updated with s1 [I-1] = s2 [J-1.

So what does a mean to make max conform to the answer?

S1 [I-1] = s2 [J-1] represents the I character of the first string and the j character of the second string, then the same character can constitute a public substring.

We can consider that if the character is preceded by a public substring, the length of the Public substring can be updated, that is, + 1.

Therefore, a [I] [j] indicates that the public substrings of the two strings are the first and the last character in the first string is the I character, the last character in the second string is the length of the Public substring of the j character.

To update the length, write the code as a [I] [j] = a [I-1] [j-1] + 1;

The statement is a [I-1] [j-1] + 1.

Code and running result
# Include
 
  
# Include
  
   
# Define N 256int f (const char * s1, const char * s2) {int a [N] [N]; int len1 = strlen (s1 ); int len2 = strlen (s2); int I, j; memset (a, 0, sizeof (int) * N); int max = 0; for (I = 1; I <= len1; I ++) {for (j = 1; j <= len2; j ++) {if (s1 [I-1] = s2 [J-1]) {a [I] [j] = a [I-1] [j-1] + 1; // fill in if (a [I] [j]> max) max = a [I] [j] ;}} return max ;}int main () {printf ("% d \ n", f ("abcdkkk ", "baabcdadabc"); return 0 ;}
  
 

(It's really easy, right? = w =)

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.