题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = scanner.nextInt(); LinkedList<MatrixSize> list = new LinkedList<>(); for (int i = 0; i < num; i++) { int rows = scanner.nextInt(); int line = scanner.nextInt(); MatrixSize matrixSize = new MatrixSize(rows, line); list.add(matrixSize); } char[] chr = scanner.next().toCharArray(); Stack<MatrixSize> stack = new Stack<>(); int result = 0; int j = 0; for (int i = 0; i < chr.length; i++) { if (chr[i] != ')') { if (chr[i] != '(') { stack.push(list.get(j)); j++; } } else { MatrixSize matrixSizeC = stack.pop(); MatrixSize matrixSizeD = stack.pop(); result += matrixSizeC.rows * matrixSizeC.line * matrixSizeD.rows; MatrixSize matrixSizeE = new MatrixSize(matrixSizeD.rows, matrixSizeC.line); stack.push(matrixSizeE); } } System.out.println(result); } } class MatrixSize { int rows; int line; public MatrixSize(int rows, int line) { this.rows = rows; this.line = line; } }