题目:

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

思路:

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

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. POJ 1060:Modular multiplication of polynomials

    Modular multiplication of polynomials Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4 ...

  2. C# WINFORM 窗体执行ORACLE存储过程 进行增删改查 自己编写借助网络(二)

    窗体界面: 下面是项目二的代码 本代码我是留着备份学习的 以供参考: 存储过程: 存储过程: 插入数据:CREATE OR REPLACE Procedure p_insert_t_cls --存储过 ...

  3. JfreeChart的使用1

    JfreeChart的使用 来自: 克洛泽大地(DREG) 2008-07-03 14:35:11 先从网上找点介绍. 一.简介 WW 的发展使得基于因特网的应用程序不再局限于静态或者简单的动态内容提 ...

  4. Java中try,catch,finally的用法

    Java中try,catch,finally的用法,以前感觉还算熟悉,但看到一篇博文才有更深点的理解,总结网友博客如下. Java异常处理的组合方式: 1.try+catch  运行流程:运行到try ...

  5. Android 性能优化(13)网络优化( 9)Determining and Monitoring the Docking State and Type

    Determining and Monitoring the Docking State and Type PreviousNext This lesson teaches you to Determ ...

  6. service: no such service mysqld 与MySQL的开启,关闭和重启

    1.问题原因与解决办法 因为修改了MySQL临时文件的目录后,使用service mysqld restart重启MySQL出现如下错误: service: no such service mysql ...

  7. cocos creator 场景如何透明,多个canvas层级显示

    转载地址:https://forum.cocos.com/t/creator-canvas/55373/14 Creator 版本:1.7 目标平台:WEB MOBILE 项目需要,页面做了多个Can ...

  8. 3122 奶牛代理商 VIII

    3122 奶牛代理商 VIII 时间限制: 3 s 空间限制: 256000 KB 题目等级 : 大师 Master       题目描述 Description 小徐是USACO中国区的奶牛代理商, ...

  9. FCC 基础JavaScript 练习6

    1.对象和数组很相似,数组是通过索引来访问和修改数据,对象是通过属性来访问和修改数据的, 对象适合用来存储结构化数据,就和真实世界的对象一模一样,比如一只猫. 任务 创建一个叫做myDog的对象,它里 ...

  10. 【Android学习笔记】Mac下Android Studio开发环境搭建

    本文由@ray 出品,转载请注明出处.  文章链接:http://www.cnblogs.com/wolfray/p/7829069.html 对于移动端这块,笔者之前一直都是进行iOS开发的,也从来 ...