题目:

给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数。

思路:

遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的次数,累加就可以了。

PS:注意弹出的矩阵表示的先后顺序。

代码:

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define MAX 1000000000
#define mod 1000000007
#define FRE() freopen("in.txt","r",stdin)
#define FRO() freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn = ;
int n;
struct MAT{
int x,y;
}mat[maxn]; int main(){
//FRE();
cin>>n;
for(int i=; i<n; i++){
char id;
int x,y;
cin>>id>>x>>y;
mat[id-'A'].x = x;
mat[id-'A'].y = y;
}
// for(int i=0; i<26; i++){
// cout<<mat[i].x<<" "<<mat[i].y<<endl;
// }
string str;
stack<MAT> sta;
while(cin>>str){
int ans=,ok=;
for(int i=; i<str.length(); i++){
if(isupper(str[i])){
sta.push(mat[str[i]-'A']);
}else if(str[i]==')'){
MAT t1 = sta.top();sta.pop();
MAT t2 = sta.top();sta.pop();
//cout<<t1.x<<" FUCK "<<t1.y<<" FUCK "<<t2.x<<" FUCK "<<t2.y<<endl;
if(t1.x!=t2.y){//注意弹出来的两个矩阵的先后顺序(栈的特点)
ok = ;
break;
}
ans += t2.x*t2.y*t1.y;//计算元素相乘的次数并累加
sta.push(MAT{t2.x,t1.y});
}
}
if(ok){
cout<<"error"<<endl;
}else{
cout<<ans<<endl;
}
while(!sta.empty())sta.pop();
}
return ;
}

UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)的更多相关文章

  1. 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 ...

  2. UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)

    意甲冠军  由于矩阵乘法计算链表达的数量,需要的计算  后的电流等于行的矩阵的矩阵的列数  他们乘足够的人才  非法输出error 输入是严格合法的  即使仅仅有两个相乘也会用括号括起来  并且括号中 ...

  3. UVa 442 Matrix Chain Multiplication(栈的应用)

    题目链接: https://cn.vjudge.net/problem/UVA-442 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...

  4. stack UVA 442 Matrix Chain Multiplication

    题目传送门 题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案 分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A ...

  5. UVa442 Matrix Chain Multiplication(栈)

    #include<cstdio>#include<cstring> #include<stack> #include<algorithm> #inclu ...

  6. 例题6-3 Matrix Chain Multiplication ,Uva 442

    这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...

  7. UVA 442 二十 Matrix Chain Multiplication

    Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  8. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  9. ACM学习历程——UVA442 Matrix Chain Multiplication(栈)

    Description   Matrix Chain Multiplication  Matrix Chain Multiplication  Suppose you have to evaluate ...

随机推荐

  1. C# winfrom TCP 服务端和客户端(链接)

    1.C#Winform TCP 之服务端: 可以参考下面链接,比较好.第二个链接可以看看,提供了一个思路. http://www.cnblogs.com/guolebin7/archive/2013/ ...

  2. android 在代码中设置字体颜色 问题

    项目中需要在代码中控制字体颜色 之前是直接引用资源文件  但是不行 tv.setTextColor(R.color.textColor_black); 无效果   后来在网上找了资料发现 要从reso ...

  3. 解决 IDEA 中文乱码

    一.打开Intellij的根目录,找到下图的两个文件(根据你的系统是32位或64位选择其中一个配置文件),在配置文件中添加:-Dfile.encoding=UTF-8 二.   配置IDE编码 点击F ...

  4. 0621补-MVC的基础整理

    包括:Model-模型.view-视图.Controller-控制器. 特点: 将功能强制分成两个部分,显示html文件,和逻辑PHP文件: 要求浏览器请求负责功能的PHP逻辑文件,该PHP逻辑文件, ...

  5. C基础-对malloc的使用与理解

    一.malloc函数分析 1.函数原型 void * malloc(size_t  size); 2.Function(功能) Allocates a block of size bytes of m ...

  6. Linux重启和关机命令

    Linux重启命令: 方式1:shutdown –r now 方式2:reboot Linux关机命令: shutdown –h now

  7. 笔记本 windows 10 安装

    开机按快捷键是F12,选择从usb启动.秋叶系统 很好用,推荐使用. 联想笔记本u深度一键u盘启动BIOS设置教程:准备工作:制作好u深度u盘启动盘http://rj.baidu.com/soft/d ...

  8. 全面学习ORACLE Scheduler特性(3)使用Programs

    二.使用Programs 在论坛中偶尔见过有人讨论如何在ORACLE中执行操作系统命令,或是ORACLE数据库外的应用.应该说在9i及之前的版本中,虽然说并非完全无法实现(其实还是有多种方式能够变相实 ...

  9. 有符号char转无符号short

    ; cout<<(int)ch<<endl; //-1 unsigned short d = ch; short dd = ch; cout<<d<<e ...

  10. jquery.autocomplete.js用法及示例,小白进

    8 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 ...