题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main() {
int n,res = 0;
cin >> n;
vector<vector<int>> matrixs(n, vector<int>(2,0));
for(int i=0; i<n; i++){
for(int j=0; j<2; j++){
cin >> matrixs[i][j];
}
}
string s;
cin >> s;
int len = s.size();
stack<vector<int>> stl;
for(int i=0; i<len; i++){
if(s[i] == '('){
stl.push({0});
} else if(s[i] == ')'){
int a,b,c;
a = stl.top()[0];
b = stl.top()[1];
stl.pop();
c = stl.top()[0];
stl.pop();
res += a * b * c;
stl.pop();
stl.push({c,b});
} else {
stl.push(matrixs[static_cast<int>(s[i] - 'A')]);
}
}
cout << res << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
对 栈 的顺序掌握度还是不够