题解 | #矩阵乘法计算量估算#

矩阵乘法计算量估算

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;
}
全部评论

相关推荐

02-01 12:05
复旦大学 Java
腾讯的提前批大概率应该是没有笔试的,但是这个时候有相当部分的同学简历估计都没有准备好,没准备好的同学也不用急,大部分都是3月之后开,这个时候开的绝大多数都是神仙打架,问的东西也比较难,打算投递的同学也多看下计算机网络和操作系统,腾讯对这部分的知识问的比较多。另外多刷下牛客的热门题库,刷题注意刷ACM模式,和牛客的周赛题,腾讯有的部门会从这里面出原题。我是@程序员花海关注我,带你了解更多校招资讯!
程序员花海:还没有来得及准备的同学可以看下学习路线:https://www.nowcoder.com/discuss/824693499982315520?sourceSSR=users算法题:https://www.nowcoder.com/feed/main/detail/20e7a999fa04485b88340a274411ca0d?sourceSSR=users八股文:https://www.nowcoder.com/discuss/833102362771251200?sourceSSR=users简历书写方式:https://www.nowcoder.com/discuss/839907820706205696?sourceSSR=users都是以前在牛客发的文章~
软开人,秋招你打算投哪些...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务