2021-03-06 Alibaba Spring Recruitment Practice Written Test Algorithm Question Record

Topic One

Reference may stay button 1411
Title Description

Xiao Ming now has 3 types of digital cards in his hand. The numbers on the cards are 1, 2, and 3. Each of the 3 types of digital cards has several cards. He wants to put these numbers in a square with N*3. Only one card can be placed in each square, and it is required that the same number of cards cannot be placed in the adjacent upper, lower, left, and right squares. Now ask you how many possible placement options are there.

Enter a description:

Enter an integer T in the first line of each file input (1<=T<=500), which means there are T groups of test data; for the
next T groups, enter an integer n (1<=n<=100000) for each group;

Output description:

For each set of test data, output an answer to represent the number of possible solutions. Since the answer is very large, please take the remainder of 10^9+7;

Example 1:

Input:
2
1
2
Output:
12
54

Description:

For the first group of the sample, put it in a 1*3 grid, the possible solutions are: (1,2,1), (2,1,2), (3,1,2), (1,2, 3), (2,1,3), (3,1,3), (1,3,1), (2,3,1), (3,2,1), (1,3,2) , (2,3,2), (3,2,3)


Code first

import java.util.Scanner;

public class Main {
    
    
    private static long[][] dp = new long[10001][2];
    private static int now = 1;
    public static void main(String[] args) {
    
    
        dp[1][0] = 6L;
        dp[1][1] = 6L;
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int i = 0 ; i < t ; i++){
    
    
            int n = sc.nextInt();
            if(n<=now){
    
    
                long res = (dp[n][0] + dp[n][1])%1000000007;
                int realRes = (int) res;
                System.out.println(realRes);
            } else {
    
    
                for(int j = now+1 ; j <= n ; j++){
    
    
                    dp[j][0] = (dp[j-1][0]*3 + dp[j-1][1]*2)%1000000007;
                    dp[j][1] = (dp[j-1][0]*2 + dp[j-1][1]*2)%1000000007;
                }
                long res = (dp[n][0] + dp[n][1])%1000000007;
                int realRes = (int) res;
                System.out.println(realRes);
                now = n;
            }
        }
    }
}

Problem-solving ideas

The first idea is to use dfs to do it, but it must be timed out. The above also suggests that the answer may be large, so dfs must not be counted one by one.

Carefully analyze the sample input given by the title, and you can find such a rule that the 2 * 3 square plan is expanded from the 1 * 3 square plan! That is to say, for the n * 3 problem, we only need to consider the last row for each solution based on the n-1 * 3 solution .

For each row of programs, we can divide them into two categories: the
first category: (1,2,1), (2,1,2), (3,1,3), (1,3, 1), (2,3,2), (3,2,3). We call this ABA type ; the
second type: (3,1,2), (1,2,3), (2,1,3), (2,3,1), (3,2, 1), (1,3,2). We call this type ABC

Let's derive the relationship between the previous row and the next row

  1. If the previous row is ABA, then there are 5 possibilities in the next row, namely (C,A,C), (B,A,B), (B,C,B), (B,A,C), (C,A,B). These five schemes are actually 3 types of ABA and 2 types of ABC;

  2. If the previous row is (A,B,C), then the next row has 4 possibilities, namely (B,A,B), (B,C,B), (B,C,A), (C ,A,B). And these four programs are actually 2 kinds of ABA and 2 kinds of ABC;

So we assume that the n * 3 problem is f(n), the number of ABA schemes is Xn, and the number of ABC schemes is Yn.

Then the recurrence relationship is as follows:
f(n+1) = Xn+1 + Yn+1;
Xn+1 = 3Xn + 2Yn;
Yn+1 = 2Xn + 2Yn;

Finally, you can use the memo dp to record the previously calculated problems to prevent double calculations and improve time efficiency.
In order to prevent overflow, the author uses the long type to calculate, and performs the remainder operation in every place (it should not be necessary).


Topic two

The second question is about graph theory, no.


to sum up

Generally speaking, the difficulty of Ali's written test questions is not low, and the type of questions is similar to CCF and ACM competition questions, and it is not the same style as the questions on Likou. Previously, I only used Likou to brush up questions, and I feel that Likou can only exercise the problem-solving thinking of algorithmic problems. It’s important to get familiar with the programming environment before the exam, otherwise it will take a long time to process the input. It is recommended that you spend this hour trying your best to solve a problem. Don’t do it in the first place. If you can’t do it, do it later. There is a high probability that you will not be able to do both of the problems. (Daniu probably wouldn’t read my blog. QAQ).

In the end, it doesn't matter if you haven't done a single question. Ali's written test and quality assessment seem to be just a reference. If you can't do it, you will have the opportunity to enter the interview. Get ready for the interview! I wish you all an early offer! Come on!

ps. If you have any questions about the interview, you can leave a message below, or you can contact me if you want Ali's introductory method!

Guess you like

Origin blog.csdn.net/weixin_43905212/article/details/114454963