Suppose you have to evaluate an expression like ABCDE where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the number of elementary multiplications needed strongly depends on the evaluation order you choose.

For example, let A be a 5010 matrix, B a 1020 matrix and C a 205 matrix. There are two different strategies to compute ABC, namely (AB)C and A(B*C).

The first one takes 15000 elementary multiplications, but the second one only 3500.

Your job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy.

Input

Input consists of two parts: a list of matrices and a list of expressions.

The first line of the input file contains one integer n (1 ≤ n ≤ 26), representing the number of matrices in the first part. The next n lines each contain one capital letter, specifying the name of the matrix, and two integers, specifying the number of rows and columns of the matrix.

The second part of the input file strictly adheres to the following syntax (given in EBNF):

SecondPart = Line { Line } < EOF>

Line = Expression < CR>

Expression = Matrix | "(" Expression Expression ")"

Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"

Output

For each expression found in the second part of the input file, print one line containing the word ‘error’ if evaluation of the expression leads to an error due to non-matching matrices. Otherwise print one line containing the number of elementary multiplications needed to evaluate the expression in the way specified by the parentheses.

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

HINT

使用map来记录矩阵,使用栈来存入数组。思路很简单,直接看代码就好。

注意:每一次做乘法计数的时候偶要判断是不是字母,否则会出错!!!

Accepted

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
using namespace std; int main()
{
map<string, vector<int>>M;
long long int sum = 0;
int n,a,b;
string t,s;
cin >> n;
while (n--) {
cin >> t >> a >> b;
M[t].push_back(a);
M[t].push_back(b);
}
getchar(); //
while (getline(cin, s)) {
stack<int>list;
sum = 0;
if (s.length() == 1)cout << 0 << endl;
else {
for (int i = 0;i < s.length();i++) {
if (s[i] == '(')continue;
if (s[i] == ')') { //计算
b = list.top();list.pop();
a = list.top();list.pop();
if (a != list.top()) { cout << "error" << endl;sum = -1;break; }
list.pop();
sum += a * b * list.top();
list.push(b);
}
else { //入栈
t = s[i];
list.push(M[t][0]);
list.push(M[t][1]);
}
}
if (sum != -1) cout << sum << endl;
}
}
}

Matrix Chain Multiplication UVA - 442的更多相关文章

  1. UVA 442 二十 Matrix Chain Multiplication

    Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  2. 例题6-3 Matrix Chain Multiplication ,Uva 442

    这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...

  3. UVA——442 Matrix Chain Multiplication

    442 Matrix Chain MultiplicationSuppose you have to evaluate an expression like A*B*C*D*E where A,B,C ...

  4. UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)

    意甲冠军  由于矩阵乘法计算链表达的数量,需要的计算  后的电流等于行的矩阵的矩阵的列数  他们乘足够的人才  非法输出error 输入是严格合法的  即使仅仅有两个相乘也会用括号括起来  并且括号中 ...

  5. Matrix Chain Multiplication[HDU1082]

    Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. UVa442 Matrix Chain Multiplication

    // UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...

  7. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  8. ACM学习历程——UVA442 Matrix Chain Multiplication(栈)

    Description   Matrix Chain Multiplication  Matrix Chain Multiplication  Suppose you have to evaluate ...

  9. UVa 442 (栈) Matrix Chain Multiplication

    题意: 给出一个矩阵表达式,计算总的乘法次数. 分析: 基本的数学知识:一个m×n的矩阵A和n×s的矩阵B,计算AB的乘法次数为m×n×s.只有A的列数和B的行数相等时,两个矩阵才能进行乘法运算. 表 ...

随机推荐

  1. hadoop环境搭建:高可用

    目录 1.硬件配置 2.软件版本 3.准备工作 3.1.配置网络环境 3.2.安装JDK 3.3.安装ZOOKEEPER 4.安装Hadoop 5.启动 6.问题 7.配置文件 1.硬件配置 采用3台 ...

  2. 微信小程序:解决小程序中有些格式如webpiPhone手机暂不支持的问题

    问题:小程序中有些格式是iPhone手机暂不支持的,如goods_introduce中的webp格式,在小程序的模拟器中是可以正常显示webp格式的,但是一旦你做真机调试,很可能某些iPhone手机是 ...

  3. Linux常用小命令

    1:查看当前磁盘内存 df-ah/df-hl 2:查看文件和文件夹大小 du -h --max-depth=1 /目的文件夹 3:scp 拷贝命令 指定端口传输文件 scp -p port filen ...

  4. Spring Boot集成Springfox Swagger3和简单应用

    摘要:Springfox Swagger可以动态生成 API 接口供前后端进行交互和在线调试接口,Spring Boot 框架是目前非常流行的微服务框架,所以,在Spring Boot 项目中集成Sp ...

  5. Service Cloud 零基础(五)Trailhead学习 Embedded Chat

    本篇参考:https://trailhead.salesforce.com/content/learn/modules/web-chat 想一下我们为什么要用service cloud呢?为什么要有s ...

  6. RocketMQ(4.8.0)——Broker 的关机恢复机制

    Broker 的关机恢复机制 一.Broker关机恢复概述 Broker关机恢复是指恢复 CommitLog.Consume Queue.Index File 等数据文件.Broker 关机分为正常调 ...

  7. 剑指 Offer 31. 栈的压入、弹出序列 + 入栈顺序和出栈顺序的匹配问题

    剑指 Offer 31. 栈的压入.弹出序列 Offer_31 题目详情: 解析: 这里需要使用一个栈来模仿入栈操作. package com.walegarrett.offer; /** * @Au ...

  8. Microsoft Teams 2021最新功能发布解读 – 会议篇

    正在进行的2021年的Microsoft Ignite大会,发布了一系列跟Microsoft Teams相关的新功能,英文介绍请参考 https://techcommunity.microsoft.c ...

  9. pytorch(04)简单的线性回归

    线性回归 线性回归是分析一个变量与另外一个变量之间关系的方法 因变量:y 自变量:x 关系:线性 y = wx+b 分析:求解w,b 求解步骤: 确定模型,Model:y = wx+b 选择损失函数, ...

  10. Linux下找出吃内存的方法总结

    Linux下查询进程占用的内存方法总结,假设现在有一个「php-cgi」的进程 ,进程id为「25282」. 现在想要查询该进程占用的内存大小.linux命令行下有很多的工具进行查看,现总结常见的几种 ...