题解 | #矩阵乘法计算量估算#
矩阵乘法计算量估算
http://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b
栈 经典四则运算改款
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <stack>
using namespace std;
int calculate(stack<pair<int, int>> &pstack) {
int z = pstack.top().second;
pstack.pop();
int y = pstack.top().second;
int x = pstack.top().first;
pstack.pop();
pstack.push({x, z});
return x * y * z;
}
int main() {
int n;
int a, b;
vector<pair<int, int>> mvec;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b;
mvec.push_back({a, b});
}
int result = 0;
string str;
cin >> str;
stack<pair<int, int>> pstack;
stack<char> ostack;
for (int i = 0; i < str.size(); i++) {
if (str[i] == '(') {
ostack.push(str[i]);
}
else if (str[i] == ')') {
result += calculate(pstack);
ostack.pop();
}
else {
int l = mvec[str[i] - 'A'].first;
int r = mvec[str[i] - 'A'].second;
pstack.push({l, r});
}
}
cout << result << endl;
return 0;
}

莉莉丝游戏公司福利 614人发布