题解 | #矩阵乘法计算量估算# 先找最左侧的')', 再找它左侧最近的'('。处理后的元素变成特殊标记。
矩阵乘法计算量估算
http://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
vscode反复debug后终于通了。
debug时希望在窗口紧挨着的变量,最好头几个字母一样。
用栈做好像很简单,后面再补充
def f(w_hs,st): num_mul=0 chrs=list(st) cids=[str(i) for i in range(len(st))] kuo_l=[] kuo_r=[] for id_c in range(len(st)): if st[id_c]=='(': kuo_l.append(id_c) w_hs.insert(id_c, 1) if st[id_c]==')': kuo_r .append(id_c) w_hs.insert(id_c, 1) n_kuo= len(kuo_l) #随便一个符号,做标记 用@比None好,debug时容易对齐 while chrs.count(None) <len(chrs) -1 : for id_p in range(n_kuo): p_right= kuo_r[id_p] - 1 chrs[kuo_r[id_p]] = None #随便一个符号,做标记 p_left= p_right - chrs[:p_right][::-1].index('(') -1 chrs[p_left ] = None # id_c+1会取到right for id_c in range(p_left,p_right): if chrs[id_c] != None: for id_c2 in range(id_c+1,p_right+1): if chrs[id_c2] != None: num_mul += (w_hs[id_c][0]*w_hs[id_c][1]) * w_hs[id_c2][1] # print('num_mul') # print(num_mul) chrs[id_c] = None w_hs[id_c2] = (w_hs[id_c][0] , w_hs[id_c2][1] ) w_hs[id_c] = None # 乘一次就break,起点右挪 break print(num_mul) while 1: try: n=int(input()) w_hs=[] for i_n in range(n): w_hs.append( tuple( map( int, input().strip().split() ) ) ) st=input().strip() f(w_hs, st) except Exception as e: if not isinstance(e,EOFError): print('!!!!!!!!!!leo_you_have_Error!!!!!!!') break