题解 | #矩阵乘法计算量估算#

矩阵乘法计算量估算

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;
            }
        }
    }
}

全部评论

相关推荐

09-28 22:01
已编辑
广西科技大学 IT技术支持
合适才能收到offe...:找桌面运维?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务