题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
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); } }