题解 | #矩阵乘法计算量估算# C++ 双栈法

矩阵乘法计算量估算

https://www.nowcoder.com/practice/15e41630514445719a942e004edc0a5b

#include <iostream>
#include <stack>
#include <utility>
#include <vector>
#include <unordered_map>
using namespace std;

int main() {
    int num;
    cin >> num;
    unordered_map<char, pair<int, int>> map;
    stack<char> st;
    stack<char> st_op;
    int x, y;
    int c = 'A';
    for (int i = 0; i < num; ++i) {
        cin >> x >> y;
        map[c++] = {x, y};
    }
    string in;
    cin >> in;

    int res = 0;
    for (char s : in) {
        if (s == '(' ) {
            st_op.push(s);
        } else if (isalpha(s)) {
            st.push(s);
        } else {
            st_op.pop();
            char matrix2 = st.top();
            st.pop();
            char matrix1 = st.top();
            st.pop();
            res += map[matrix1].first * map[matrix2].second * map[matrix1].second;
            map[matrix1].second = map[matrix2].second;
            st.push(matrix1);
        }
    }
    cout << res << endl;
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
07-16 14:10
门头沟学院 Java
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
不对是145个人…嗯…&nbsp;大家都没发现秋招提前批来了嘛..笑死我了
牛客39712426...:投了也是浪费时间,之前投米实习,除了浪费我时间写笔试题没有任何反馈,懒得投了
26届校招投递进展
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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