java-HJ70_矩阵乘法计算量估算

java-HJ70 矩阵乘法计算量估算

import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while(in.hasNext()){
            solution1(in);
            solution2(in);
        }
    }

    /**
     * stack
     * @param in
     */
    private static void solution1(Scanner in){
        int num = in.nextInt();

        int[][] matrixRC = new int[num][2];
        for(int i=0; i<num; i++){
            matrixRC[i][0] = in.nextInt();
            matrixRC[i][1] = in.nextInt();
            in.nextLine();
        }

        String rule = in.nextLine();

        int result = 0;
        Stack<Integer> rcStack = new Stack<>();
        for(int i=0; i<rule.length(); i++){
            char ch = rule.charAt(i);
            if(ch == '('){
                continue;
            }else if(ch == ')'){
                // 右矩阵列数
                int RC = rcStack.pop();
                // 右矩阵行数
                int RR = rcStack.pop();
                // 左矩阵列数
                int LC = rcStack.pop();
                // 左矩阵行数
                int LR = rcStack.pop();

                result += LR * LC * RC;

                rcStack.push(LR);
                rcStack.push(RC);
            }else{
                rcStack.push(matrixRC[ch-'A'][0]);
                rcStack.push(matrixRC[ch-'A'][1]);
            }
        }

        System.out.println(result);
    }


    /**
     * stack + map
     */
    private static void solution2(Scanner in){
        int num = Integer.parseInt(in.nextLine());

        // 矩阵名->矩阵行列数
        HashMap<String, String> matrixMap = new HashMap<>();
        for(int i=0; i<num; i++){
            matrixMap.put(String.valueOf((char) ('A'+i)), in.nextLine());
        }

        String rule = in.nextLine();

        int result = 0;
        Stack<String> matrix = new Stack<>();
        for(int i=0; i<rule.length(); i++){
            char ch = rule.charAt(i);
            if(ch == '('){
                continue;
            }else if(ch == ')'){
                // 右矩阵
                String matrixR = matrix.pop();
                // 左矩阵
                String matrixL = matrix.pop();
                // 右矩阵行列数
                String[] matrixRRC = matrixMap.get(matrixR).split(" ");
                // 左矩阵行列数
                String[] matrixLRC = matrixMap.get(matrixL).split(" ");
                
                result += Integer.parseInt(matrixLRC[0]) * Integer.parseInt(matrixLRC[1]) * Integer.parseInt(matrixRRC[1]);
                
                matrixMap.put(matrixL+matrixR, matrixLRC[0]+" "+matrixRRC[1]);
                matrix.push(matrixL+matrixR);
            }else{
                matrix.push(String.valueOf(ch));
            }
        }

        System.out.println(result);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值