描述 | |
---|---|
知识点 | 字符串 |
运行时间限制 | 10M |
内存限制 | 128 |
输入 | 输入多行,先输入要计算乘法的矩阵个数n,每个矩阵的行数,列数,总共2n的数,最后输入要计算的法则 3 //矩阵个数n |
输出 | 输出需要进行的乘法次数 |
样例输入 | 3 50 10 10 20 20 5 (A(BC)) |
样例输出 | 3500 |
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <map>
#include <algorithm>
using namespace std;
struct matrix
{
int row;
int col;
};
void main()
{
vector<matrix> vec;
stack<matrix> sta;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
matrix tmp;
cin >> tmp.row >> tmp.col;
vec.push_back(tmp);
}
string str;
cin >> str;
int result = 0;
for (int i = 0; i < str.length(); i++)
{
if (str[i] >= 'A'&&str[i] <= 'Z')
{
int index = str[i] - 'A';
sta.push(vec[index]);
}
else if (str[i] == ')')
{
matrix value1 = sta.top();
sta.pop();
matrix value2 = sta.top();
sta.pop();
matrix value3;
value3.row = value2.row;
value3.col = value1.col;
sta.push(value3);
result += value2.row*value2.col*value1.col;
}
}
cout << result;
}