2019 Nanjing University Computer Science Department Undergraduate Open Day Topics

2019/7/12, local group

This is a question based on the memories of freshmen. The expression method may not be rigorous, and the data range is not given. It is only for reference.

In addition, the time complexity below belongs to the author's ideas and does not represent the AC solution.

1. Given a string S, consisting of () and []. Among them, the value of () and [] is 1, the value of AB is A+B, the value of (A) is A*2, the value of [A] is A+1, and if the brackets are not matched, -1 is output. Find the value represented by S.

This is a small question on simulating polynomial calculations, focusing on bracket matching. Due to the existence of (A), this question requires pushing the value onto the stack while matching the brackets. Since the length of the string is not known, there may be a pitfall of 2^31 exploding int.

(Later, classmate AC learned that this question did not crack the int)

Time complexity O(n)

2. Given an array A, its subscripts are 1~n, where the i-th number A[i] represents the position that can be jumped to in the next step (for example, A[1]=3, then you can jump from position 1 to Position 3), each operation can choose to add A[i] + 1, or -1, or skip a step, and ask for the minimum number of operations required to go from position 1 to position n.

Sample 1

enter

5
3 4 2 5 3

output

3

explain

Add A[1]+1, +1, then jump to 5.

At first glance, this problem seems to be a 3^n pruning backtracking, but grid skipping problems generally have various simplifications, and experience tells us that there are better solutions.

The difficulty of this question is how to analyze the problem and turn it into a known problem.

First, the problem is to find the minimum number of steps. Each position corresponds to a fixed next position, so the same position cannot be passed twice. Then the process of changing A[i] and jumping is equivalent to moving between adjacent positions after jumping. Therefore, the operation in the question can be modified as follows: for each operation, you can choose to jump one step; when it is located at a position other than position 1, you can choose to move one square to the adjacent position.

At this point, the problem can be expressed graphically and bfs can be applied.

Time complexity O(n)

3. A person has N yuan, M books, and A[i] yuan for each book. How many books can he buy with no more than N yuan, and how many combinations can buy the most books. N, M<500

Sample 1

enter

5 4
2 3 3 4

output

2 2

dp knapsack problem, for A[i], dp[n][m]+=dp[n-A[i]][m-1], n<=N, record the maximum m=maxm. Output dp[i][maxm], the sum of i<=N.

The time complexity is O(NM^2), which feels a bit stupid. I don’t know if it has been optimized.


2019/7/16, field team

Question source:https://blog.csdn.net/HanielF/article/details/95677016

Occasionally, I saw that there were also questions from out-of-town groups, so I wrote down my thoughts.

The threshold for the questions for the non-local group this year is indeed much higher, a little more difficult than for local students. ww

1. The general meaning is that, given you a number n that does not exceed 100 digits, and a number k that does not exceed 100, you are required to remove k digits from the number n, and then make n the smallest after removing k numbers.

Reference link for problem solution:https://blog.csdn.net/C20190413/article/details/77368590

In fact, it can be traversed in one go, with time complexity O(n)

2. The general meaning is that there are B boys and G girls. All boys and girls are required to line up in a line. The number of consecutive boys cannot exceed K. How many ways are there in total?

This question can be regarded as (G+1) drawers. Each drawer can hold up to K items. There are B items in total. Ask about different ways to put them. In this way, it is easy to think of the DP solution to this problem.

dp[g][b]+=dp[g-1][b-i], 0<=i<min(b, k), where dp stores the possible number of b boys that have been used when inserting position g. dp[0][0]=1, output dp[G+1][B]

(It turns out there is DP every year)

Applying double pointers can reduce the traversal complexity, and the time complexity is O(BG)

3. Given a pre-order traversal sequence and a post-order traversal sequence of a binary tree, there is no empty node # number in the sequence, only letters. How many different binary trees can be constructed through these two sequences, because the trees look different. The traversed sequence may be the same. For example, the preorder sequence: abc, the postorder sequence cba, there are 4 different trees.

The pre-order traversal sequence and the post-order traversal sequence can determine the connection relationship of a tree. However, in a binary tree, if a node has only one child node, the left and right positions of the child node cannot be determined. Therefore, the goal can be turned to count the number of nodes a with only one child node, and output 2^a.

The traversal method is relatively simple and will not be written here.

Worst time complexity O(n^2)

Guess you like

Origin blog.csdn.net/hizcard/article/details/95601101