java算法巩固训练day02

乘积最大

  • 给出一个n位数,在数字中间添加k个乘号,使得最终的乘积最大。
  • 非记忆化版本!!!
package Test.ACM;

import java.io.*;
import java.util.*;
import java.math.*;

/*

4  2
1231

4 1
6666

*/

public class Main {
    public static void main(String[] args) {
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        while(in.hasNext()) {
            int n = in.nextInt();
            int m = in.nextInt();
            String tmp = in.next();
            char[] str = tmp.toCharArray();
            BigInteger[][] dp = new BigInteger[n + 1][m + 1];
            BigInteger[][] sum = new BigInteger[n + 1][n + 1];
            BigInteger now;
            BigInteger zero = BigInteger.ZERO;
            BigInteger ten = BigInteger.TEN;
            for(int i = 0; i <= n; ++i) { // 注意要初始化,否则创建的BigInteger类对象为null,计算过程中会出现错误
                for(int j = 0; j <= n; ++j) {
                    sum[i][j] = zero;
                }
                for(int j = 0; j <= m; ++j) {
                    dp[i][j] = zero;
                }
            }
            for(int i = 1; i <= n; ++i) {
                sum[i][i] = new BigInteger(String.valueOf(str[i - 1]));
                for(int j = i + 1; j <= n; ++j) {
                    now = new BigInteger(String.valueOf(str[j - 1]));
                    sum[i][j] = sum[i][j - 1].multiply(ten).add(now);
                }
            }
            for(int i = 1; i <= n; ++i) { // 前i位插入0个乘号即为前i位数字组成的数
                dp[i][0] = sum[1][i];
            }
            for(int i = 1; i <= n; ++i) { // 枚举第i位
                for(int j = 1; j <= Math.min(i - 1, m); ++j) { // 枚举使用第j个括号,最大不超过i-1或者k,也就是在第i位后面放一个乘号
                    for(int k = j; k < i; ++k) { // 枚举在位置[k, i)
                        now = dp[k][j - 1].multiply(sum[k + 1][i]);
                        int dif = dp[i][j].compareTo(now);
                        if(dif < 0) {
                            dp[i][j] = now;
                        }
                    }
                }
            }
            System.out.println(dp[n][m]);
        }
        out.close();
    }
}
class InputReader {
    BufferedReader buf;
    StringTokenizer tok;
    InputReader() {
        buf = new BufferedReader(new InputStreamReader(System.in));
    }
    boolean hasNext() {
        while(tok == null || !tok.hasMoreElements()) {
            try {
                tok = new StringTokenizer(buf.readLine());
            }
            catch(Exception e) {
                return false;
            }
        }
        return true;
    }
    String next() {
        if(hasNext()) return tok.nextToken();
        return null;
    }
    int nextInt() {
        return Integer.parseInt(next());
    }
    long nextLong() {
        return Long.parseLong(next());
    }
    double nextDouble() {
        return Double.parseDouble(next());
    }
    BigInteger nextBigInteger() {
        return new BigInteger(next());
    }
    BigDecimal nextBigDecimal() {
        return new BigDecimal(next());
    }
}
  • 记忆化搜索版本:定义dp[i][j] 表示前i位使用j个乘号相乘得到的最大值。dfs一般是放一个乘号就减一个,递归边界是k==0,同时记忆化子答案,这样按着递推的公式就能轻松写出代码!
package Test.ACM;

import java.io.*;
import java.util.*;
import java.math.*;

/*

4  2
1231

4 1
6666

*/

public class Main {
    static BigInteger[][] dp = new BigInteger[45][10];
    static BigInteger[][] sum = new BigInteger[45][45];
    static int n;
    static BigInteger negOne = new BigInteger("-1");
    public static void main(String[] args) {
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        while(in.hasNext()) {
            n = in.nextInt();
            int m = in.nextInt();
            String tmp = in.next();
            char[] str = tmp.toCharArray();
            BigInteger now;
            BigInteger zero = BigInteger.ZERO; // 0
            BigInteger ten = BigInteger.TEN; // 10
            for(int i = 0; i <= n; ++i) { // 注意要初始化,否则创建的BigInteger类对象为null,计算过程中会出现错误
                for(int j = 0; j <= n; ++j) {
                    sum[i][j] = zero;
                }
                for(int j = 0; j <= m; ++j) {
                    dp[i][j] = negOne; // 初始化为-1,做记忆化搜索
                }
            }
            for(int i = 1; i <= n; ++i) {
                sum[i][i] = new BigInteger(String.valueOf(str[i - 1]));
                for(int j = i + 1; j <= n; ++j) {
                    now = new BigInteger(String.valueOf(str[j - 1]));
                    sum[i][j] = sum[i][j - 1].multiply(ten).add(now);
                }
            }
            System.out.println(dfs(n, m));
        }
        out.close();
    }
    public static BigInteger dfs(int d, int k) {
        if(k == 0) return dp[d][k] = sum[1][d]; // 边界条件:剩下0个括号
        if(dp[d][k].compareTo(negOne) > 0) return dp[d][k]; // 记忆化搜索
        BigInteger now;
        int dif;
        for(int j = k; j < d; ++j) { // 在区间[k, d) 每个位置放一个括号
            now = dfs(j, k - 1).multiply(sum[j + 1][d]);  
            dif = dp[d][k].compareTo(now);
            if(dif < 0) dp[d][k] = now;
        }
        return dp[d][k]; // 返回结果
    }
}
class InputReader {
    BufferedReader buf;
    StringTokenizer tok;
    InputReader() {
        buf = new BufferedReader(new InputStreamReader(System.in));
    }
    boolean hasNext() {
        while(tok == null || !tok.hasMoreElements()) {
            try {
                tok = new StringTokenizer(buf.readLine());
            }
            catch(Exception e) {
                return false;
            }
        }
        return true;
    }
    String next() {
        if(hasNext()) return tok.nextToken();
        return null;
    }
    int nextInt() {
        return Integer.parseInt(next());
    }
    long nextLong() {
        return Long.parseLong(next());
    }
    double nextDouble() {
        return Double.parseDouble(next());
    }
    BigInteger nextBigInteger() {
        return new BigInteger(next());
    }
    BigDecimal nextBigDecimal() {
        return new BigDecimal(next());
    }
}

动态规划DP

  • POJ 3176 Cow Bowling
  • 记忆化搜索
package Test.ACM;

import java.io.*;
import java.util.*;
import java.math.*;

/*

定义:dp[i][j]表示前i行第j列已走路径和的最大值
显然,对于第i层的第j个位置,往下看,只有2个选择,那么递推公式为
dp[i][j] = a[i][j] + max(dp[i + 1][j], dp[i + 1][j + 1])
非递归从后往前遍历,相当于深搜的回溯
递归要从前往后看,递归到底层,回溯从后往前统计,注意使用记忆化搜索,不然会造成重复计算大量相同子问题

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

*/

public class Main {
    static int n;
    static int[][] mp = new int[400][400];
    static int[][] dp = new int[400][400];
    public static void main(String[] args) {
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        while(in.hasNext()) {
            n = in.nextInt();
            for(int i = 0; i <= n; ++i) {
                for(int j = 0; j <= n; ++j) {
                    mp[i][j] = 0; // 初始化为0
                    dp[i][j] = -1; // 初始化为-1,用于记忆化搜索
                }
            }
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= i; ++j) {
                    mp[i][j] = in.nextInt();
                }
            }
            System.out.println(dfs(1, 1)); // 站在最高点往下搜索
        }
        out.close();
    }
    public static boolean check(int x, int y) {
        if(x < 1 || y < 1 || x > n || y > n) return false;
        return true;
    }
    public static int dfs(int x, int y) {
        if(!check(x, y)) return 0;
        else if(dp[x][y] != -1) return dp[x][y];
        else return dp[x][y] = mp[x][y] + Math.max(dfs(x + 1, y), dfs(x + 1, y + 1));
    }
}
class InputReader {
    BufferedReader buf;
    StringTokenizer tok;
    InputReader() {
        buf = new BufferedReader(new InputStreamReader(System.in));
    }
    boolean hasNext() {
        while(tok == null || !tok.hasMoreElements()) {
            try {
                tok = new StringTokenizer(buf.readLine());
            }
            catch(Exception e) {
                return false;
            }
        }
        return true;
    }
    String next() {
        if(hasNext()) return tok.nextToken();
        return null;
    }
    int nextInt() {
        return Integer.parseInt(next());
    }
    long nextLong() {
        return Long.parseLong(next());
    }
    double nextDouble() {
        return Double.parseDouble(next());
    }
    BigInteger nextBigInteger() {
        return new BigInteger(next());
    }
    BigDecimal nextBigDecimal() {
        return new BigDecimal(next());
    }
}
  • 非递归版本
package Test.ACM;

import java.io.*;
import java.util.*;
import java.math.*;

/*

定义:dp[i][j]表示前i行第j列已走路径和的最大值
显然,对于第i层的第j个位置,往下看,只有2个选择,那么递推公式为
dp[i][j] = a[i][j] + max(dp[i + 1][j], dp[i + 1][j + 1])
非递归从后往前遍历,相当于深搜的回溯
递归要从前往后看,递归到底层,回溯从后往前统计,注意使用记忆化搜索

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

*/

public class Main {
    static int n;
    static int[][] mp = new int[400][400];
    static int[][] dp = new int[400][400];
    public static void main(String[] args) {
        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);
        while(in.hasNext()) {
            n = in.nextInt();
            for(int i = 0; i <= n; ++i) {
                for(int j = 0; j <= n; ++j) {
                    dp[i][j] = mp[i][j] = 0;
                }
            }
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= i; ++j) {
                    mp[i][j] = in.nextInt();
                }
            }
            for(int i = n; i > 0; --i) {
                for(int j = 1; j <= i; ++j) {
                    dp[i][j] = mp[i][j] + Math.max(dp[i + 1][j], dp[i + 1][j + 1]);
                }
            }
            System.out.println(dp[1][1]);
        }
        out.close();
    }
}
class InputReader {
    BufferedReader buf;
    StringTokenizer tok;
    InputReader() {
        buf = new BufferedReader(new InputStreamReader(System.in));
    }
    boolean hasNext() {
        while(tok == null || !tok.hasMoreElements()) {
            try {
                tok = new StringTokenizer(buf.readLine());
            }
            catch(Exception e) {
                return false;
            }
        }
        return true;
    }
    String next() {
        if(hasNext()) return tok.nextToken();
        return null;
    }
    int nextInt() {
        return Integer.parseInt(next());
    }
    long nextLong() {
        return Long.parseLong(next());
    }
    double nextDouble() {
        return Double.parseDouble(next());
    }
    BigInteger nextBigInteger() {
        return new BigInteger(next());
    }
    BigDecimal nextBigDecimal() {
        return new BigDecimal(next());
    }
}

你可能感兴趣的:(java算法巩固训练day02)