题目链接: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. go实例之线程池

    go语言使用goroutines和channel实现一个工作池相当简单.使用goroutines开指定书目线程,通道分别传递任务和任务结果.简单的线程池代码如下: package main impor ...

  2. 用vue实现简单分页

    在这个demo中,我用vue对一个json文件中的数据进行了简单的分页,没用用到交互,一下是我的实现过程. 基础逻辑 1.将json文件引入app.vue,并作为data返回 data(){ var ...

  3. 关于文件上传的ajax交互

    首先我们来了解一下上传文件 <input type="file"> input的file常用上传类型 后缀名 MIME名称 *.3gpp audio/3gpp, vid ...

  4. Python-String字符串的相关方法

  5. 【转】nginx提示:500 Internal Server Error错误的解决方法

    本文转自:http://www.jb51.net/article/35675.htm 现在越来越多的站点开始用 Nginx ,("engine x") 是一个高性能的 HTTP 和 ...

  6. IDLE3.6.3 Mac版不支持中文输入解决办法

    最近安装了IDLE 3.6.3版本 但是在IDLE中要输入中文注释时发现虽然输入法切换到了中文,但输入的还是英文.然后我在IDLE外试了下,输入中文没问题,于是就确认应该是IDLE的问题. 网上查询到 ...

  7. Git详解之九:Git内部原理

    Git 内部原理 不管你是从前面的章节直接跳到了本章,还是读完了其余各章一直到这,你都将在本章见识 Git 的内部工作原理和实现方式.我个人发现学习这些内容对于理解 Git 的用处和强大是非常重要的, ...

  8. VS2017 调试不能命中断点问题

    去掉勾

  9. Benchmark Test On Android Devices

    一.Android设备上的Benckmark测试概述 同PC相比,在Android设备上的性能测试还没有一个公认的标准.也没有PC上那么多的测试程序集.但我们可以通过一些工具所得到的信息更好的了解设备 ...

  10. zabbix 图形插件 Grafana的安装

    看http://www.myexception.cn/software-testing/2008870.html 就好了.