题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
import functools import itertools import sys def multipleTimes(s1,s2): ret= s1[0] * s2[1] * s1[1] if len(s1) > 2: ret += s1[2] if len(s2) > 2: ret += s2[2] return ret multipleShapes = lambda s1, s2: [s1[0], s2[1], multipleTimes(s1, s2)] numbers = int(input()) shapes = [] rules = "" for line in sys.stdin: if len(shapes) < numbers: shapes.append([int(each) for each in line.split()]) else: rules = line break assert rules.count("(") == rules.count(")") rule_iter = iter(rules) def count_unit(pre_shapes): ret = functools.reduce(multipleShapes, pre_shapes) return ret def count_times(rule_iter, pre_shapes): while True: try: tmp = next(rule_iter) if tmp >= "A" and tmp <= "Z": pre_shapes.append(shapes[int(ord(tmp) - ord('A'))]) elif tmp == ")": return count_unit(pre_shapes) elif tmp == "(": unit_shapes = [] pre_shapes.append(count_times(rule_iter, unit_shapes)) else: pass except StopIteration as e: return pre_shapes = [] res = count_times(rule_iter,pre_shapes) print(pre_shapes[0][2])