Matrix Chain Multiplication

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2015-08-25)

Description

 

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

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 <stdio.h>
#include <string.h>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std; struct Node
{
int a;
int b;
}; stack <Node> s; int main()
{
Node m[];
int n;
int i,j,k,flg;
char nu[],str[];
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
{
scanf("%s",nu);
k=nu[]-'A';
scanf("%d %d",&m[k].a,&m[k].b);
} while(scanf("%s",str)!=EOF)
{
if(!s.empty())
s.pop();
int l=strlen(str),final=;;
flg=;
for(i=;i<l;i++)
{
if(isalpha(str[i]))
s.push(m[str[i]-'A']);
else if(str[i]==')')
{
Node m1,m2,mm;
m2=s.top();s.pop();
m1=s.top();s.pop();
if(m1.b!=m2.a)
{
flg=;
break;
}
final=final+m1.b*m1.a*m2.b;
mm.a=m1.a,mm.b=m2.b;
s.push(mm);
}
}
if(flg==)
printf("error\n");
else
printf("%d\n",final);
}
}
return ;
}

最新文章

  1. BZOJ 1123 BLO
  2. java实现的kmp算法
  3. Quart 2D 绘制图形简单总结
  4. POJ-2296 Map Labeler 2sat
  5. Rendering Transparent 3D Surfaces in WPF with C#(转载)
  6. linux(centos)上配置nginx、mysql、php-fpm、redis开机启动&lt;转&gt;
  7. .NET抽象工厂模式微理解--教你在项目中实现抽象工厂
  8. IIS7中JS、CSS、Image无法显示和加载解决方案
  9. DDOS学习笔记(《破坏之王-DDOS攻击与防范深度剖析》)
  10. 4层板的pcb创建
  11. template.helper()方法
  12. 快速搭建WebAPI(Odata+Code-First)附Odata条件查询表~
  13. Django 中的Form、ModelForm
  14. Qt程序ibus输入法不跟随
  15. 转载CSDN博客步骤
  16. Shell脚本编程基础笔记二
  17. 我的学习工作经历,一个园林专业中专毕业生的IT之路 学习编程 创业
  18. Python学习---模拟微信网页登录180410
  19. 【AUC】二分类模型的评价指标ROC Curve
  20. PHP开启页面报错的代码

热门文章

  1. java 网络编程(五)----TCP进阶篇上传文本文件
  2. 转:MyEclipse8.6插件安装方法
  3. Java图像文件的读写
  4. Android NDK 开发(四)java传递数据到C【转】
  5. iOS学习之Table View的简单使用
  6. PHP array_count_values() 函数用于统计数组中所有值出现的次数。
  7. textarea 在浏览器中禁用拖动和固定大小
  8. POJ 3237:Tree(树链剖分)
  9. mysql之使用xtrabackup进行物理备份、恢复、在线克隆从库、在线重做主从
  10. 使用ResourceBundle访问资源文件(properties)帮助类