题目链接: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

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
 
Source
 
题意:这里介绍一种写结构体重构造函数比较神奇的写法;详见刘汝佳大神的代码
题解:一道水题,注意表达式求值的题应当马上想到用栈来实现
自己的ac代码

 #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(表达式求值用栈操作)的更多相关文章

  1. 【NYOJ-35】表达式求值——简单栈练习

    表达式求值 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...

  2. 表达式求值(栈方法/C++语言描述)(二)

    上篇中完成了对表达式求值的整体过程,接下来看看如何处理不同类型的token. 对运算数的处理比较简单,它直接调用函数strtod(),将字符串中的运算数转换为浮点类型并将它压入运算数栈中: void ...

  3. C语言之四则运算表达式求值(链栈)—支持浮点型数据,负数, 整型数据运算

     运算符间的优先级关系: 链栈结构体定义: 数据域使用字符串长度为20的字符数组(故需要注意判断读取的字符串是运算符还是数值) 可支持浮点型数据,负数, 整型数据的运算 float EvaluateE ...

  4. 河南省acm第九届省赛--《表达式求值》--栈和后缀表达式的变形--手速题

    表达式求值 时间限制:1000 ms | 内存限制:65535 KB 难度:3   描述 假设表达式定义为:1. 一个十进制的正整数 X 是一个表达式.2. 如果 X 和 Y 是 表达式,则 X+Y, ...

  5. 表达式求值(栈方法/C++语言描述)(一)

    一个算数表达式(以下简称为表达式)由运算数.运算符.左括号和右括号组成,定义一个枚举类型TokenType表示为: typedef enum { BEGIN, NUMBER, OPERATOR, LE ...

  6. UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)

    栈的练习,如此水题竟然做了两个小时... 题意:给出矩阵大小和矩阵的运算顺序,判断能否相乘并求运算量. 我的算法很简单:比如(((((DE)F)G)H)I),遇到 (就cnt累计加一,字母入栈,遇到) ...

  7. 洛谷P1981 表达式求值 题解 栈/中缀转后缀

    题目链接:https://www.luogu.org/problem/P1981 这道题目就是一道简化的中缀转后缀,因为这里比较简单,只有加号(+)和乘号(*),所以我们只需要开一个存放数值的栈就可以 ...

  8. 表达式求值(栈方法/C++语言描述)(三)

    代码清单 // calculator.h #ifndef CALCULATOR_H #define CALCULATOR_H #include <stack> #include <s ...

  9. 双栈算术表达式求值算法 栈(Stack) - Java实现

    https://mp.weixin.qq.com/s/dg8mgd6CIQ7Tui1_fQwSBA https://github.com/toywei/DataStructure/tree/maste ...

随机推荐

  1. 1_3 C语言解决求n!

    求n!(n为键盘输入的任意整数值).要求分别用while语句和for语句实现 用while语句实现: #include <stdio.h> int main() { int n; scan ...

  2. 一个APP页面一个接口

    目前所在的公司做的是健康产业方面的APP,这个产品包括了安卓和IOS还有web三方面,除了要写后台管理的系统外,还要写移动端的接口.第一次写移动端接口就犯了一个错误,以为和web一样是怎么方便怎么来, ...

  3. JPA实体类注解、springboot测试类、lombok的使用

    前提准备: 搭建一个springboot项目,详情请参见其它博客:点击前往 1 引入相关依赖 web.mysql.jpa.lombok <?xml version="1.0" ...

  4. bzoj 3528: [Zjoi2014]星系调查

    Description 银河历59451年,在银河系有许许多多已被人类殖民的星系.如果想要在行 星系间往来,大家一般使用连接两个行星系的跳跃星门.  一个跳跃星门可以把 物质在它所连接的两个行星系中互 ...

  5. form表单与后台请求的关系

    开发中遇到一个问题,说这个问题前先看一下代码 后台方面, get请求: post请求: 前端方面: 问题是:当我点击提交表单后,页面会跳转成这样: 经过多番测试,原因竟是form表单的提交问题,如果用 ...

  6. 微信公众号H5支付遇到的那些坑

    简史 官方文档说的很清楚,商户已有H5商城网站,用户通过消息或扫描二维码在微信内打开网页时,可以调用微信支付完成下单购买的流程. 当然,最近微信支付平台也加入了纯H5支付,也就是说用户可以在微信以外的 ...

  7. C# DataGridView中DataGridViewComboBoxCell列,下拉框事件的处理【完美解决】

    http://blog.csdn.net/a312100321/article/details/25195311 问题:DataGridView绑定数据源之后,有一列需要用下拉框DataGridVie ...

  8. Web程序员们,你准备好迎接HTML5了吗?

    HTML5作为下一代的web开发标准,其特性已经慢慢地出现在主流的浏览器中,这种新的HTML将会让浏览器不必再依赖Flash.QuickTime.Silverlight等插件,也简化了原来需要大量JS ...

  9. 使用linux perf工具生成java程序火焰图

    pre.cjk { font-family: "Nimbus Mono L", monospace } p { margin-bottom: 0.1in; line-height: ...

  10. TP框架如何开启log日志

    1. 日志的处理工作是由系统自动进行的,在开启日志记录的情况下,会记录下允许的日志级别的所有日志信息. 其中,为了性能考虑,SQL日志级别必须在调试模式开启下有效,否则就不会记录. 系统的日志记录由核 ...