题目链接: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. 简单介绍什么是协程及其在ES6中的实现方式

    协程,英文名coroutine,是一种执行过程可以被暂停和恢复的方法.各个协程之间相互协作完成一个任务. 让我们来看一个关于发挥协程作用的例子.假定我们有一个生产者和消费者的关系,生产者创建物品并将物 ...

  2. Xamarin android使用Sqlite做本地存储数据库

    android使用Sqlite做本地存储非常常见(打个比方就像是浏览器要做本地存储使用LocalStorage,貌似不是很恰当,大概就是这个意思). SQLite 是一个软件库,实现了自给自足的.无服 ...

  3. lesson - 14 linux系统日常管理3

    1. Linux系统服务管理工具ntsysv 类似图形界面管理工具,如果没有该命令使用 yum install -y ntsysv 安装常用服务:crond, iptables, network, s ...

  4. Java自己动手写连接池三

    Java自己动手写连接池三,核心代码; package com.kama.cn; import java.sql.Connection;import java.util.ArrayList;impor ...

  5. 树链剖分( 洛谷P3384 )

    我们有时候遇到这样一类题目,让我们维护树上路径的某些信息,这个时候发现我们无法用线段树或者树状数组来维护这些信息,那么我们就有着一种新的数据结构,树剖:将一棵树划分成若干条链,用数据结构去维护每条链, ...

  6. vue2.0表单事件的绑定

    v-model 1.input type="text" <template> <div id="app"> <label for= ...

  7. Head First设计模式之访问者模式

    一.定义 定义:表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作. 访问者模式适用于数据结构相对稳定的系统, 它把数据结构和作用于数据结构之上的操 ...

  8. Git与GitHub学习笔记(八)git如何同时同步提交到码云和GitHub上

    前言: 今天github push代码一直push不上去,打算就备份一份代码带国内开源码云上. Github容易出现的情况是: 国内访问速度比较慢, 如果被墙掉的话,就直接没发使用了 如果开源个PHP ...

  9. Redis锁构造

    单线程与隔离性 Redis是使用单线程的方式来执行事务的,事务以串行的方式运行,也就是说Redis中单个命令的执行和事务的执行都是线程安全的,不会相互影响,具有隔离性. 在多线程编程中,对于共享资源的 ...

  10. iOS自带API集成二维码、条形码扫描

    源码于 :https://github.com/wangjinfeng/ScanForiOSAPI/tree/main 1.AVFoundation.framework,QuartzCore.fram ...