题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
const n = parseInt(readline()) const matrixArr = [] for(let i = 0;i<n;i++){ matrixArr.push(readline().split(' ').map(Number)) } const str = readline() const cal = (mat1,mat2) =>{ return mat1[0]*mat1[1]*mat2[1] } const oppArr = [] const numArr = [] let res = 0 for(let i = 0;i<str.length;i++){ if(str[i]==='('){ oppArr.push('(') } if(/[A-Z]/.test(str[i])){ numArr.push(str[i]) } if(str[i] === ')'){ if(oppArr[oppArr.length-1]==='('){ const index1 = numArr.pop().charCodeAt()-65 const index2 = numArr[numArr.length-1].charCodeAt()-65 const mat1 = matrixArr[index2] const mat2 = matrixArr[index1] res += cal(mat1,mat2) matrixArr[index2] = [matrixArr[index2][0],matrixArr[index1][1]] oppArr.pop() } } } console.log(res)