Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082
Matrix Chain Multiplication
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1382 Accepted Submission(s): 905
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.
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"
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))
0
0
error
10000
error
3500
15000
40500
47500
15125
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
using namespace std;
#define N 30
#define M 50000
struct Mnode{
int m;
int n;
}mm[N];
Mnode stk[M]; int main()
{
int t;
while(~scanf("%d",&t))
{
map<char,int> mp;
for(int i = ; i < t; i++)
{
char ch;
int m, n;
getchar();
scanf("%c %d %d",&ch,&m,&n);
mp[ch] = i;
mm[i].m = m;
mm[i].n = n;
}
char ml[];
while(~scanf("%s",ml))
{
int top = ;
int ans = ;
bool flag = ;
for(int i = ; i < strlen(ml); i++)
{
if(ml[i]<='Z'&&ml[i]>='A'){
Mnode tm;
tm.m = mm[mp[ml[i]]].m;
tm.n = mm[mp[ml[i]]].n;
stk[top++] = tm;
}
else if(ml[i]==')'){
Mnode tm1,tm2,tm3;
tm2 = stk[--top];
tm1 = stk[--top];
if(tm1.n!=tm2.m){ puts("error"); flag = ; break; }
tm3.m = tm1.m;
tm3.n = tm2.n;
ans+=tm1.m*tm1.n*tm2.n;
stk[top++] = tm3;
}
}
if(flag) printf("%d\n",ans);
}
}
return ;
}
刘汝佳大神的代码,注意其中的构造函数的写法,表示如果没有参数的时候自动默认两个参数值都是0
#include<cstdio>
#include<stack>
#include<iostream>
#include<string>
using namespace std;
struct Matrix{
int a, b;
Matrix(int a = , int b = ):a(a),b(b){}
}m[];
stack<Matrix> s; int main()
{
int n;
cin>>n;
for(int i = ; i < n; i++){
string name;
cin>>name;
int k = name[]-'A';
cin>>m[k].a>>m[k].b;
}
string expr;
while(cin>>expr){
int len = expr.length();
bool error = false;
int ans = ;
for(int i = ; i < len; i++){
if(isalpha(expr[i])) s.push(m[expr[i]-'A']);
else if(expr[i]==')'){
Matrix m2 = s.top();s.pop();
Matrix m1 = s.top();s.pop();
if(m1.b!=m2.a){error = true; break;}
ans += m1.a*m1.b*m2.b;
s.push(Matrix(m1.a,m2.b));
}
}
if(error) printf("error\n"); else printf("%d\n",ans);
}
return ;
}
Matrix Chain Multiplication(表达式求值用栈操作)的更多相关文章
- 【NYOJ-35】表达式求值——简单栈练习
表达式求值 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...
- 表达式求值(栈方法/C++语言描述)(二)
上篇中完成了对表达式求值的整体过程,接下来看看如何处理不同类型的token. 对运算数的处理比较简单,它直接调用函数strtod(),将字符串中的运算数转换为浮点类型并将它压入运算数栈中: void ...
- C语言之四则运算表达式求值(链栈)—支持浮点型数据,负数, 整型数据运算
运算符间的优先级关系: 链栈结构体定义: 数据域使用字符串长度为20的字符数组(故需要注意判断读取的字符串是运算符还是数值) 可支持浮点型数据,负数, 整型数据的运算 float EvaluateE ...
- 河南省acm第九届省赛--《表达式求值》--栈和后缀表达式的变形--手速题
表达式求值 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 假设表达式定义为:1. 一个十进制的正整数 X 是一个表达式.2. 如果 X 和 Y 是 表达式,则 X+Y, ...
- 表达式求值(栈方法/C++语言描述)(一)
一个算数表达式(以下简称为表达式)由运算数.运算符.左括号和右括号组成,定义一个枚举类型TokenType表示为: typedef enum { BEGIN, NUMBER, OPERATOR, LE ...
- UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)
栈的练习,如此水题竟然做了两个小时... 题意:给出矩阵大小和矩阵的运算顺序,判断能否相乘并求运算量. 我的算法很简单:比如(((((DE)F)G)H)I),遇到 (就cnt累计加一,字母入栈,遇到) ...
- 洛谷P1981 表达式求值 题解 栈/中缀转后缀
题目链接:https://www.luogu.org/problem/P1981 这道题目就是一道简化的中缀转后缀,因为这里比较简单,只有加号(+)和乘号(*),所以我们只需要开一个存放数值的栈就可以 ...
- 表达式求值(栈方法/C++语言描述)(三)
代码清单 // calculator.h #ifndef CALCULATOR_H #define CALCULATOR_H #include <stack> #include <s ...
- 双栈算术表达式求值算法 栈(Stack) - Java实现
https://mp.weixin.qq.com/s/dg8mgd6CIQ7Tui1_fQwSBA https://github.com/toywei/DataStructure/tree/maste ...
随机推荐
- JDBC 程序实例小练习
JDBC 程序实例问题 编程实现如下功能:在数据库中建立一个表,表名为student,其结构为学号.姓名.性别.年龄.英语.JavaSE程序设计.初级日语.总分,在表中输入多条记录. 学生的总分信息, ...
- laravel and lumen 软删除操作
知识都是有联系的,这绝对是真理.作为一名小白,看了一点官方文档,把我自己理解的软删除操作给大家讲讲.有些就是套用官方文档的话. 定义:什么是软删除呢,所谓软删除指的是数据表记录并未真的从数据库删除,而 ...
- 框架原理第三讲,RTTCreate,运行时类型创建.(以MFC框架讲解)
框架原理第三讲,RTTCreate,运行时类型创建.(以MFC框架讲解) 通过昨天的讲解,我们已经理解了运行时类型识别是什么. 比如 CObject * pthis = (Cobject *)Cre ...
- 改造 Combo Select支持服务器端模糊搜索
项目中使用了 combo select,为缺省的select增加模糊搜索的功能,一直运行得很好. 1 碰到的问题 但最近碰到一个大数据量的select:初始化加载的数据项有2000多个.我们采用 ...
- 【ASP.NET MVC系列】浅谈NuGet在VS中的运用
一 概述 在我们讲解NuGet前,我们先来看看一个例子. 1.例子: 假设现在开发一套系统,其中前端框架我们选择Bootstrap,由于选择Bootstrap作为前端框架,因此,在项目中,我们 ...
- MySQL Replication 主从复制全方位解决方案
1.1 主从复制基础概念 在了解主从复制之前必须要了解的就是数据库的二进制日志(binlog),主从复制架构大多基于二进制日志进行,二进制日志相关信息参考:http://www.cnblogs.com ...
- Java 向下转型
1.Java 中父类直接向子类转型的不合法的,可以编译但运行时报错. Java中子类直接向父类转型 是合法的,但转型后,可以执行的方法仅限存在于父类中的,在执行时,先看子类的是否有定义,有就执行,没有 ...
- How to setup a DL4J project with eclipse
https://electronsfree.blogspot.com/2016/10/how-to-setup-dl4j-project-with-eclipse.html
- 基于阿里云的JavaEE系统框架介绍
基于阿里云的系统框架展望 1) CDN 用于缓存静态文件等等.七牛和阿里的都还可以. 七牛要做的久一点,各种图片处理的接口要完善一些 阿里的CDN要稍微好一点点,但是没有不安全的访问方式,访问稍微没有 ...
- Sequelize 基本操作
Sequelize 是 Node 的一个 ORM(Object-Relational Mapping) 框架,用来方便数据库操作. 配置 sequelize 以 mysql 为例 首先我们要引入npm ...