题目:

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

思路:

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

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. Linux服务器 /var/spool/clientmqueue 目录下产生大量文件的删除办法

    检查linux发现server中的磁盘分区空间超过98%,登录到服务器查看 [root@localhost etc]# df -hFilesystem 容量 已用 可用 已用% 挂载点/dev/hda ...

  2. 容器ConcurrentHashMap原理(学习)

    一.概述 HashMap 是非线程安全的,在不考虑性能问题的时候,我们的解决方案有 Hashtable 或者Collections.synchronizedMap(hashMap),这两种方式基本都是 ...

  3. Explicit Interface Implementation (C# Programming Guide)

    https://msdn.microsoft.com/en-us/library/ms173157.aspx If a class implements two interfaces that con ...

  4. 获得了Root权限后Read-only file system

    获得了Root权限后,adb shell进入文件系统,有时仍然不能对系统文件夹进行写操作,典型的如删除/system/app下的Apk, 例如系统报:rm failed for xxx.apk, Re ...

  5. mysql与mongoDB的特点和优劣

    首先分析下mysql与mongoDB的特点和优劣 从图中分析: 再来分析下应用场景: a.如果需要将mongodb作为后端db来代替mysql使用,即这里mysql与mongodb 属于平行级别,那么 ...

  6. zoj 3023 Light Bulb

    题目大意: 求L的最大值 思路: 可以想象出是一个关于人到灯泡距离x的单峰上凸函数 当光线在墙角左边的时候影子在不断增长 然后通过相似可以推出人在墙上影子的长度为:H+D*(h-H)/x 再加上地上的 ...

  7. POJ1259 The Picnic 最大空凸包问题 DP

    POJ1259 给定平面上100个点 求一个最大的凸包,使得它不包含其中任意点,且凸包的顶点是题目所给的点. 枚举凸包左下角的点,顺时针枚举第二个点, 用opt[i][j]记录 i作为第二个点, 且第 ...

  8. Python基础Web服务器案例

    一.WSGI 1.PythonWeb服务器网关接口(Python Web Server Gateway Interface,缩写为WSGI) 是Python应用程序或框架和Web服务器之间的一种接口, ...

  9. bzoj 1596: [Usaco2008 Jan]电话网络【贪心】

    dfs,如果一个点的儿子.本身.父亲都没有塔,就在父亲上建一个 原理不明-- #include<iostream> #include<cstdio> using namespa ...

  10. JavaScript编程艺术-第8章-8.6.1-显示“缩略词语表”

    8.6.1-显示“缩略词语表” ***代码亲测可用*** HTML: JS: ***end***