题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int N = Integer.parseInt(in.nextLine()); int[][] res = new int[N][2]; for (int i = 0; i < N; i++) { String[] strings = in.nextLine().split(" "); res[i][0] = Integer.parseInt(strings[0]); res[i][1] = Integer.parseInt(strings[1]); } String str = in.nextLine().toUpperCase(); Stack<int[]> stack = new Stack<>(); int sum = 0; int i = 0; for (char b : str.toCharArray()) { if (b == '(') continue; else if (b >= 'A' && b <= 'Z') { stack.push(res[i]); i++; } else { int[] last = stack.pop(); int[] first = stack.pop(); sum = sum + first[0] * first[1] * last[1]; int[] ne = new int[2]; ne[0] = first[0]; ne[1] = last[1]; stack.push(ne); } } System.out.println(sum); } } }