Sample Input
9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
A
B
C
(AA)
(AB)
(AC)
(A(BC))
((AB)C)
(((((DE)F)G)H)I)
(D(E(F(G(HI)))))
((D(EF))((GH)I))
Sample Output
0
0
0
error
10000
error
3500
15000
40500
47500
15125
在输入中第一个数n表示矩阵的个数;接下来n行输入系列矩阵的名字,行和列;然后输入指令,就是括号和一些表矩阵的字母;要你用矩阵的运算法则求每个指令用了多少乘法;如果不能乘法就输出error;
思路;输入矩阵信息时,把矩阵名也就是字母作为map的key,矩阵的行和列作为map的value,然后只要调用矩阵名就能得到矩阵的行,列;这里map相当于一个数组;接下来用字符串接受指令,并开一个栈,当指令是左括号或字母时入,右括号时出三个元素,前两个是矩阵名,后一个是左括号;然后形成新的矩阵,并且用新的字母作为矩阵名入map
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <map>
using namespace std;
typedef struct {char ch;int r,c;}matix;
matix s[40];
int main()
{
char T[26];
for(int i=0;i<26;i++)T[i]='a'+i;
int n;map<char,pair<int,int> >s;
//freopen("1.txt","r",stdin);
while(cin>>n)
{
for(int i=0;i<n;i++)
{
char c;int a,b;
cin>>c>>a>>b;
s.insert(make_pair(c,make_pair(a,b)));
}
s.insert(make_pair('ch',make_pair(0,0)));
string str;
while(cin>>str)
{
if(str.length()==1)cout<<"0\n";
else
{
stack<char>fu;int co=0;int flag=0;
for(int i=0;i<(int)str.length();i++)
{
if(str[i]=='('||isalpha(str[i]))fu.push(str[i]);
else if(str[i]==')')
{
char te1=fu.top();fu.pop();char te2=fu.top();fu.pop();fu.pop();fu.push(T[i]);
if(s[te2].second==s[te1].first)
{
co+=s[te2].first*s[te2].second*s[te1].second;
s[T[i]].first=s[te2].first,s[T[i]].second=s[te1].second;
}
else {flag=1;break;}
}
}
if(flag)cout<<"error\n";
else
cout<<co<<endl;
}
}
}
}