题解 | #二叉树#

二叉树

https://www.nowcoder.com/practice/aaefe5896cce4204b276e213e725f3ea

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException  {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StreamTokenizer in = new StreamTokenizer(br);
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        while (in.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int) in.nval;
            in.nextToken();
            int m = (int) in.nval;
            long[][] dp = new long[n + 1][m + 1];
            for (int i = 0; i <= n; i++) {
                for (int j = 0; j <= m; j++) {
                    dp[i][j] = -1;
                }
            }
            out.println(compute2(n, m, dp));
        }
        out.flush();
        out.close();
        br.close();
    }

    // 记忆化搜索动态规划
    private static long compute(int n, int m, long[][] dp) {
        if (n == 0) {
            return 1;
        }
        if (m == 0) {
            return 0;
        }
        if (dp[n][m] != -1) {
            return dp[n][m];
        }

        long ans = 0;
        for (int i = 0; i < n; i++) {
            ans = (ans + compute(i, m - 1, dp) * compute(n - i - 1, m - 1,
                    dp)) % 1000000007;
        }
        dp[n][m] = ans;
        return ans;
    }

    // 严格位置依赖的动态规划
    private static long compute2(int n, int m, long[][] dp) {
        for (int j = 0; j <= n; j++) {
            dp[j][0] = 0;
        }
        for(int i=0; i<= m; i++){
            dp[0][i] = 1; 
        }
        for(int i=1; i<= n; i++){
            for(int j=1; j<=m; j++){
                dp[i][j] = 0;
                for(int k=0; k < i; k++) {
                    dp[i][j] = (dp[i][j] + dp[k][j-1] * dp[i-k-1][j-1]%1000000007)%1000000007;
                }
            }
        }
        return dp[n][m];
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 18:54
点赞 评论 收藏
分享
躺尸修仙中:因为很多92的也去卷中小厂,反正投递简历不要钱,面试不要钱,时间冲突就推,不冲突就面试积累经验
点赞 评论 收藏
分享
10-27 17:26
东北大学 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务