题解 | #矩阵乘法计算量估算#

矩阵乘法计算量估算

https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static class Matrix {

        public int row;

        public int col;

        public Matrix(int row, int col) {
            this.row = row;
            this.col = col;
        }
    }

    /**
     * 3
     * 50 10
     * 10 20
     * 20 5
     * (A(BC))
     * 3500
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Matrix> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int row = sc.nextInt();
            int col = sc.nextInt();
            list.add(new Matrix(row, col));
        }
        String expression = sc.next();
        Deque<Matrix> stack = new ArrayDeque<>();
        int ans = 0;
        for (int i = 0; i < expression.length(); i++) {
            char ch = expression.charAt(i);
            if (Character.isLetter(ch)) {
                stack.push(list.get(ch - 'A'));
            } else if (ch == ')') {
                Matrix m1 = stack.pop();
                Matrix m2 = stack.pop();
                ans += m2.row * m2.col * m1.col;
                stack.push(new Matrix(m2.row, m1.col));
            }
        }
        System.out.println(ans);
    }
}

全部评论

相关推荐

去B座二楼砸水泥地:不过也可以理解,这种应该没参加过秋招
点赞 评论 收藏
分享
牛客868257804号:九个中铁八个中建
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务