题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
import java.util.*;
public class Main {
static int res = 0;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] xy = new int[n][2];
for(int i = 0;i < n;i++){
xy[i][0] = in.nextInt();
xy[i][1] = in.nextInt();
}
String next = in.next();
order(next,0,0,xy);
System.out.println(res);
}
public static void order(String s,int x,int y,int[][] xy){
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] >= 'A' && chars[i] <= 'Z') {
if (x == 0 && y == 0) {
x = xy[chars[i] - 'A'][0];
y = xy[chars[i] - 'A'][1];
} else {
res += x * xy[chars[i] - 'A'][1] * y;
y = xy[chars[i] - 'A'][1];
}
} else if (chars[i] == '(') {
int count = 1;
int j = i + 1;
char start = 'a';
char end = chars[j];
while (count > 0) {
if (chars[j] == '(') {
count++;
} else if (chars[j] == ')') {
count--;
}else{
if(start == 'a'){
start = chars[j];
}
end = chars[j];
}
j++;
}
int y2 = xy[end - 'A'][1];
res += x*y*y2;
order(s.substring(i + 1, j-1), 0, 0, xy);//j-1是反括号
if(x==0){
x = xy[start - 'A'][0];
}
y = y2;
i = j-1;
}
}
}
}
查看14道真题和解析