UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)
题目:
给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数。
思路:
遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的次数,累加就可以了。
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(栈模拟水题+专治自闭)的更多相关文章
- 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 /* 问题 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 解题思路 栈的应用,直接忽视左括号, ...
- stack UVA 442 Matrix Chain Multiplication
题目传送门 题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案 分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A ...
- UVa442 Matrix Chain Multiplication(栈)
#include<cstdio>#include<cstring> #include<stack> #include<algorithm> #inclu ...
- 例题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 ...
- 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 ...
随机推荐
- Lein droid
最近尝试使用Clojure,发现有个Lein droid的项目可以方便的在android下使用Clojure. http://clojure-android.info/#get-started 尝试了 ...
- BaezaYates 交集python和golang代码
def bsearch(find, arr, low, high): while low <= high: mid = (low + high) >> 1 if arr[mid] = ...
- 洛谷P1514 引水入城——dfs
题目:https://www.luogu.org/problemnew/show/P1514 搜索+DP: 自己想出来的方法第一次80分好高兴! 再改了改就A了,狂喜乱舞: 也就是 dfs,仔细一想第 ...
- vue 简介
Vue.js是一套构建用户界面的渐进式框架.采用自底向上增量开发的设计.Vue的核心值关注视图层非常容易与其他库和已有的项目整合.另外,Vue完全有能力驱动采用单文件组件和Vue生态系统支持的库开发复 ...
- 16. Ext.ux.TabCloseMenu插件的使用(TabPanel右键关闭菜单) 示例
转自:https://crabdave.iteye.com/blog/327978 Ext.ux.TabCloseMenu插件的使用(TabPanel右键关闭菜单) 示例 效果: 创建调用的HTML: ...
- centos7的systemd
系统启动流程 POST --> Boot Sequence --> Bootloader --> kernel+initramfs(initrd) --> rootfs --& ...
- bzoj 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生【dp】
参考:http://hzwer.com/3917.html 好神啊 注意到如果分成n段,那么答案为n,所以每一段最大值为\( \sqrt{n} \) 先把相邻并且值相等的弃掉 设f[i]为到i的最小答 ...
- JS 实现PDF文件打印
function PdfPrint() { bdhtml = window.document.body.innerHTML; sprnstr = "<!-- ...
- C#结构体+结构体与类的区别
C# 结构(Struct) 在 C# 中,结构是值类型数据结构.它使得一个单一变量可以存储各种数据类型的相关数据.struct 关键字用于创建结构. C# 结构的特点 您已经用了一个简单的名为 Boo ...
- EditText(4)常用属性详解
常用的属性: 显示密码 通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码. editText.setTransformationMethod( ...