题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
http://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
int n = Integer.parseInt(sc.nextLine());
int[][] num = new int[n][2];
for (int i = 0; i < n; i++) {
String[] arr = sc.nextLine().split(" ");
num[i][0] = Integer.parseInt(arr[0]);
num[i][1] = Integer.parseInt(arr[1]);
}
String msg = sc.nextLine();
Stack<Integer> z = new Stack<>();
int count = 0;
for (int j =0; j < msg.length(); j++) {
char tmp = msg.charAt(j);
if (tmp == '(') {
continue;
} else if (tmp == ')') {
int cc = z.pop();
int cr = z.pop();
int bc = z.pop();
int br = z.pop();
count += br * cc * bc;
z.push(br);
z.push(cc);
} else {
int index = tmp - 'A';
z.push(num[index][0]);
z.push(num[index][1]);
}
}
System.out.println(count);
}
}
}