题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
核心思想将 规则 入栈维护
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let source = [] rl.on('line', function (line) { source.push(line.trim()) }); rl.on('close', function () { let n = Number(source.shift()) let dict = 'ABCDEFGHIJKLMNO' let rule = source.pop() let idx = 0 // console.log(source) // 将规则按栈维护 rule = rule.split('') let stack = [] let res = 0 for (let i = 0; i < rule.length; i++) { if (rule[i] == ")") { let b = stack.pop() let a = stack.pop() // 弹出括号 stack.pop() // 累加结果 res += a[0] * a[1] * b[1] // 封装新的值 stack.push([a[0], b[1]]) } else if (/[A-Z]/.test(rule[i])) { let idx = dict.indexOf(rule[i]) // console.log(idx) stack.push(source[idx].split(' ')) } else { stack.push(rule[i]) } } console.log(res) })