编写程序计算不同的计算顺序需要进行的乘法次数
输入描述:
输入多行,先输入要计算乘法的矩阵个数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;
}