矩阵乘法计算量估算

编写程序计算不同的计算顺序需要进行的乘法次数

 

 

 

 


输入描述:

输入多行,先输入要计算乘法的矩阵个数n,每个矩阵的行数,列数,总共2n的数,最后输入要计算的法则



输出描述:

输出需要进行的乘法次数


输入例子:
3
50 10
10 20
20 5
(A(BC))

输出例子:
3500

//3 50 10 10 20 20 5 (A(BC))
//3500
//3 50 10 10 20 20 5 (ABC)
//15000
#include<iostream>
#include<stack>
#include<map>
#include<vector>
using namespace std;
int main(){
    int n;
    char c='A';
    stack<char> sc;
    map<char,pair<int,int>> mcpii;
    while(cin>>n){
        c='A';
        while(!sc.empty())
            sc.pop();
        mcpii.clear();
        pair<int,int> temp;
        for(int i=0;i<n;i++){
            cin>>temp.first>>temp.second;
            mcpii[c++]=temp;
        }
        string s;
        stack<char> sctemp;
        cin>>s;
        int ct=0;
        char after,before;
        for(auto ch:s){
            if(ch==')'){
                //当未在1、2、3处加上判断是否为空之前,一直通不过,加上后,就过了。
                while(!sc.empty() && sc.top()!='('){//1
                    sctemp.push(sc.top());
                    sc.pop();
                }
                if(!sc.empty() && sc.top()=='(')//2
                    sc.pop();//弹出'('
                if(!sctemp.empty()){//3
                    before=sctemp.top();
                    sctemp.pop();
                    while(!sctemp.empty()){
                        after=sctemp.top();
                        sctemp.pop();
                        ct+=mcpii[before].first*mcpii[before].second*mcpii[after].second;
                        mcpii[before].second=mcpii[after].second;
                    }
                    sc.push(before);
                }
            }
            else{
                sc.push(ch);
            }
        }
        cout<<ct<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值