博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ_1094/hdoj_1082_Matrix Chain Multiplication_...
阅读量:7185 次
发布时间:2019-06-29

本文共 2567 字,大约阅读时间需要 8 分钟。

hot3.png

Matrix Chain Multiplication

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 893    Accepted Submission(s): 603
Problem Description
Matrix multiplication problem is a typical example of dynamical programming. 
Suppose you have to evaluate an expression like A*B*C*D*E 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 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix.
There are two different strategies to compute A*B*C, namely (A*B)*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

#include 
#include
#include
#include
#include
using namespace std; struct Node{ int row; int col;}; stack
array; map
matrix;int n;char name;string exp;int main(){ cin>>n; for(int i=0; i
>name; cin>>matrix[name].row>>matrix[name].col; } while(cin>>exp){ int count=0; int i=0; for(; i

转载于:https://my.oschina.net/dianpaopao/blog/125709

你可能感兴趣的文章
android 自动下载apk,Android 带进度条自动下载Apk并自动安装
查看>>
android中如何通知系统回收对象,Android操作系统之内存回收策略
查看>>
三星谷歌Android7,台版三星S7系列已推送Android7.0 国行版不远了
查看>>
android5中常见布局,使用Kotlin开发Android应用(5) - 常用的布局控件
查看>>
国内 ios android 苹果 安卓 市场份额 2013年,谈什么追赶苹果iOS?Android9.0发布3个月市场份额几乎为零!...
查看>>
android studio的jks,获取*.jks签名的方法(Android studio)
查看>>
html 文本框 onchange,[原创]关于html页面中Input(文本框)控件OnChange事件的触发条件...
查看>>
dom操作插入html代码,DOM操作
查看>>
django html超链接传参数,在django模板中实现超链接配置
查看>>
面试如何让自己赢在细节
查看>>
HyperV2012的学习,从这里开始
查看>>
云原生与云原生应用概念解析
查看>>
创业成功的关键是能够找到合适的合伙人
查看>>
FireEye:2012年下半年高级威胁分析报告
查看>>
2018世界杯决赛:谁的选择多谁就会赢球!
查看>>
程序员教你如何追女生
查看>>
哈夫曼树构造算法的正确性证明
查看>>
我谈Web程序难测试
查看>>
nginx日志按照天进行分割
查看>>
Networker 8.1异机恢复Oracle 11gR2
查看>>