博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zoj-1094Matrix Chain Multiplication(栈来实现矩阵相乘)
阅读量:4049 次
发布时间:2019-05-25

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

Matrix Chain Multiplication

Time Limit: 2 Seconds     
Memory Limit: 65536 KB

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 Specification

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 } 
Line = Expression
Expression = Matrix | "(" Expression Expression ")"Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"

Output Specification

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

9A 50 10B 10 20C 20 5D 30 35E 35 15F 15 5G 5 10H 10 20I 20 25ABC(AA)(AB)(AC)(A(BC))((AB)C)(((((DE)F)G)H)I)(D(E(F(G(HI)))))((D(EF))((GH)I))

Sample Output

000error10000error3500150004050047500 15125 需要对字符进行处理 如果是'('就跳过 如果是')'就将)前的两个矩阵取出 判断先前的矩阵的行数与后一个的矩阵的列数是否相同 如果不同就输出error 再将乘之后的矩阵继续放回栈中 #include
#include
#include
#include
#include
using namespace std;struct node{int l,r;};int main(){ ios::sync_with_stdio(false); map
m; int n;char a; cin>>n; for(int i=1;i<=n;i++) { cin>>a; cin>>m[a].l>>m[a].r; } string s; while(cin>>s) { stack
u; int count=0; int i; for(i=0;i

转载地址:http://lafci.baihongyu.com/

你可能感兴趣的文章
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>
使用 Springboot 对 Kettle 进行调度开发
查看>>
一文看清HBase的使用场景
查看>>
解析zookeeper的工作流程
查看>>
搞定Java面试中的数据结构问题
查看>>
慢慢欣赏linux make uImage流程
查看>>
linux内核学习(7)脱胎换骨解压缩的内核
查看>>
以太网基础知识
查看>>
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux phy驱动初始化2
查看>>
慢慢欣赏linux CPU占用率学习
查看>>