题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
import java.util.Scanner import java.util.* fun main(args: Array<String>) { val read = Scanner(System.`in`) val n = read.nextLine().toInt() val a = Array(n) { Array(2) { 0 } } for (i in 0 until n) { val res = read.nextLine().split(' ').map { it.toInt() } a[i][0] = res[0] a[i][1] = res[1] } val rule = read.nextLine() val stackVal = Stack<Pair<Int, Int>>() var sum = 0 rule.forEach { if (it.isLetter()) { a[it - 'A'].apply { stackVal.push(Pair(get(0), get(1))) } } else { when (it) { ')' -> { val curVal = stackVal.pop() val preVal = stackVal.pop() sum += preVal.first * curVal.second * preVal.second stackVal.push(Pair(preVal.first, curVal.second)) } } } } println(sum) }