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

矩阵乘法计算量估算

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);
    }
}

全部评论

相关推荐

Ncsbbss:又想干活又想要工资,怎么什么好事都让你占了
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 10:56
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务