博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva-442 Matrix Chain Multiplication
阅读量:6237 次
发布时间:2019-06-22

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

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 ( tex2html_wrap_inline28 ), 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

 

000error10000error350015000405004750015125
题目意思:给出字符代表矩阵的定义,计算表达示中矩阵相乘的总次数;
解题思路:定义一个stack
st,用node(-1,-1)代表左括号,从左到右扫描,如果是左括号则st.push(node(-1,01));如果是字母则push字母对应的矩阵;如果是右括号则把矩阵pop出来计算,一直到左括号;
#include 
#include
#include
#include
#include
#include
#include
#include
using namespace std;//bool check (vector
&v,map
&m)//{// for(unsigned int i=1;i
>cas;// getchar();// while(cas--)// {// int n;// cin>>n;// getchar();// vector
v;// string s;// map
m;// for(int i=0; i
0; j--)// v[j]=v[j-1];// v[0]=t;// flag=0;// q=0;// break;// }// }// if(flag)q++;// }// cout<
>n; char c; int x,y; node arr[200]; for(int i=0; i
>c>>x>>y,arr[c]=node(x,y); string exp; while(cin>>exp) { stack
st; int sum=0; bool flag=0; for(int i=0; i
 

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

你可能感兴趣的文章
Qt 删除目录
查看>>
Git 移除某些文件
查看>>
poj2940
查看>>
django做form表单的数据验证
查看>>
【OpenFOAM】——OpenFOAM入门算例学习
查看>>
STL UVA 11991 Easy Problem from Rujia Liu?
查看>>
模拟 URAL 1149 Sinus Dances
查看>>
Oracle 11G 数据库迁移【expdp/impdp】
查看>>
17.EXTJs 中icon 与iconCls的区别及用法!
查看>>
3.mybatis实战教程(mybatis in action)之三:实现数据的增删改查
查看>>
Caused by: Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar
查看>>
让你拥有超能力:程序员应该掌握的统计学公式
查看>>
互联网组织的未来:剖析 GitHub 员工的任性之源
查看>>
Java 开源博客 Solo 1.4.0 发布 - 简化
查看>>
Oracle巡检
查看>>
【转载】胜者树
查看>>
查看mysql数据库存放的路径|Linux下查看MySQL的安装路径
查看>>
selenium+testNG+Ant
查看>>
1024程序员节,你屯书了吗?(内含福利)
查看>>
移动端JS 触摸事件基础
查看>>