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

矩阵乘法计算量估算

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

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

int main() {
    int n;
    cin >> n;
    vector<int> row(n);
    vector<int> col(n);
    for(int i = 0; i < n; ++i) {
        cin >> row[i] >> col[i];
    }
    string expression;
    cin >> expression;
    stack<char> st;
    int ans = 0;
    for(const char& c: expression) {
        if(c != '(' && c != ')') st.push(c);
        else if(c == ')') {
            char c1 = st.top(); st.pop();
            char c2 = st.top(); st.pop();
            ans += row[c2-'A'] * row[c1-'A'] * col[c1-'A'];
            st.push(c1);
            row[c1-'A'] = row[c2-'A'];//让B = A*B  所以更新B的行数 方便后续计算
        }
    }
    cout << ans << endl;
    return 0;
}

全部评论

相关推荐

有担当的灰太狼又在摸鱼:零帧起手查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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