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

矩阵乘法计算量估算

https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b

alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

while 1:
    try:
        # 输入
        n = int(input())
        d = {}
        for i in range(n):
            d[alphabet[i]] = list(map(int, input().split()))
        order = input()
        # 运算
        stack = []  # 用栈解决运算优先级
        r = 0
        for i in order:
            if i != ')':
                stack.append(i)
            else:
                a2, a1 = stack.pop(), stack.pop()
                stack.pop()  # 弹出'('
                r += d[a1][0] * d[a1][1] * d[a2][1]
                d[a1] = [d[a1][0], d[a2][1]]  # 参数是什么都无所谓
                stack.append(a1)
        print(r)
    except:
        break

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务