stack UVA 442 Matrix Chain Multiplication
题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案
分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A * B 是a * c。遇到')'弹出两个矩阵相乘,错误的话直接break
收获:以前做过了,现在会表达式求值后,这题也太容易了
代码:
/************************************************
* Author :Running_Time
* Created Time :2015-8-29 10:22:51
* File Name :UVA_442.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Martrix {
int a, b;
Martrix (int _a = 0, int _b = 0) : a (_a), b (_b) {};
}m[26]; int main(void) {
int n;
string name;
cin >> n;
for (int i=1; i<=n; ++i) {
cin >> name; int k = name[0] - 'A';
cin >> m[k].a >> m[k].b;
} string exp;
stack<Martrix> S;
while (cin >> exp) {
bool error = false;
int ans = 0;
int len = exp.length ();
for (int i=0; i<len; ++i) {
if ('A' <= exp[i] && exp[i] <= 'Z') S.push (m[exp[i]-'A']);
else if (exp[i] == ')') {
Martrix m1 = S.top (); S.pop ();
Martrix m2 = S.top (); S.pop ();
if (m2.b != m1.a) {
error = true; break;
}
ans += m2.a * m2.b * m1.b;
S.push (Martrix (m2.a, m1.b));
}
}
if (error) cout << "error" << endl;
else cout << ans << endl;
} return 0;
}
stack UVA 442 Matrix Chain Multiplication的更多相关文章
- UVA——442 Matrix Chain Multiplication
442 Matrix Chain MultiplicationSuppose you have to evaluate an expression like A*B*C*D*E where A,B,C ...
- UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)
意甲冠军 由于矩阵乘法计算链表达的数量,需要的计算 后的电流等于行的矩阵的矩阵的列数 他们乘足够的人才 非法输出error 输入是严格合法的 即使仅仅有两个相乘也会用括号括起来 并且括号中 ...
- UVa 442 Matrix Chain Multiplication(栈的应用)
题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...
- UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)
题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的 ...
- 例题6-3 Matrix Chain Multiplication ,Uva 442
这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...
- UVA 442 二十 Matrix Chain Multiplication
Matrix Chain Multiplication Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- UVa442 Matrix Chain Multiplication
// UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- ACM学习历程——UVA442 Matrix Chain Multiplication(栈)
Description Matrix Chain Multiplication Matrix Chain Multiplication Suppose you have to evaluate ...
随机推荐
- Spring常用的接口和类(二)
七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostPro ...
- 给定一个值S,在有序数组中找出两个元素A和B,使 A+B = S.
在网上看到过一个面试题,感觉挺有意思,看别人的代码写的逻辑不够谨慎,重写了一个,较真了又... package com.array7.algorithm; public class Algorithm ...
- OpenSwitch操作系统成为Linux基金会官方项目
导读 非盈利机构Linux基金会为推进Linux和开源软件在企业和专业人士的发展,于今天宣布OpenSwitch项目成为Linux基金会官方项目之一. Linux基金会的常务董事Jim Zemlin表 ...
- 鸟哥的linux私房菜学习笔记 __ 命令与文件的搜寻
连续输入两次[tab]按键就能够知道使用者有多少命令可以下达.那你知不知道这些命令的完整档名放在哪里?举例来说,ls 这个常用的命令放在哪里呢? 就透过 which 或 type 来找寻吧! 范例一: ...
- Struts2配置文件模板
<?xml version = "1.0" encoding = "UTF-8"?><!--下面是Struts2配置文件的DTD信息 --&g ...
- c# 继承,多态,new /overrid 区别, 引用父类的方法
好久没碰c#了,偶尔需要制作点小工具.为了一个灵活的架构设计,需要对继承/多态有比较深刻的理解. 不料忘得差不多了,好吧,再来回忆下.直接上代码了,如下: using System; using Sy ...
- JAVA程序1,1,2,3,5,8,13,21....第30个是什么...?
解题思路:从第3个数字开始,后一个数字是前2个数字的和public class text{ public static void main(String[] args) { int num1=1,nu ...
- 【OpenStack】OpenStack系列14之Dashboard定制开发
django概述 参考资料:http://blog.javachen.com/2014/01/11/how-to-create-a-django-site.html http://djangobook ...
- HashMap实现原理分析(详解)
1. HashMap的数据结构 http://blog.csdn.net/gaopu12345/article/details/50831631 ??看一下 数据结构中有数组和链表来实现对数据的存 ...
- Intersection of Two Arrays | & ||
Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example ...