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

while True:
    try:
        N = int(input())
        lst = []
        for i in range(N):
            lst.append(list(map(int, input().split())))
        str_1 = input()
        opstr = []
        count = 0
        stack_1 = []
        for token in str_1:
            if token != ")":
                stack_1.append(token)
            elif token == ")":
                toptoken1 = stack_1[-1]
                toptoken2 = stack_1[-2]
                if toptoken1 != "(" and toptoken2 != "(":
                    op2 = stack_1.pop()
                    op1 = stack_1.pop()
                    op3=  stack_1.pop()
                    nu_1 = lst[ord(op1) - 65]
                    nu_2 = lst[ord(op2) - 65]
                    row_1 = nu_1[0]
                    col_1 = nu_1[1]
                    row_2 = nu_2[0]
                    col_2 = nu_2[1]
                    count += row_1 * col_1 * col_2
                    a = len(lst)
                    lst.append([row_1, col_2])
                    stack_1.append(chr(a + 65))
                else:
                    op4 = stack_1.pop()
                    op5 =stack_1.pop()
        print(count)
    except:
        break

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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