【链接】 我是链接,点我呀:)

【题意】

在这里输入题意

【题解】

用栈来处理一下表达式就好。
因为括号是一定匹配的。所以简单很多。
a*b x b*c会做a*b*c次乘法。

【代码】

#include<bits/stdc++.h>
#define ll long long
using namespace std; const int N = 26; int n;
char s[5];
pair <ll, ll> v[300],v1[300];
stack <char> sta; string cl(string s)
{
for (int i = 'A'; i <= 'Z'; i++) v[i] = v1[i];
if (s[0] != '(') return "0";
int len = s.size();
ll ans = 0;
while (!sta.empty()) sta.pop();
for (int i = 0; i < len; i++)
{
if (s[i] == '(')
{
sta.push(s[i]);
}else
if (s[i] == ')')
{
char b = sta.top();
sta.pop();
char a = sta.top();
sta.pop();
sta.pop();//'('删掉
ll x = v[a].first, y = v[a].second,z = v[b].second;
if (y != v[b].first) return "error";
ans += x*y*z;
v[a].first = x, v[a].second = z;
sta.push(a);
}
else
{
sta.push(s[i]);
}
}
string temp = "";
while (ans)
{
temp = char(ans % 10 +'0')+ temp;
ans /= 10;
}
return temp;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%s", s);
int x, y;
scanf("%d%d", &x, &y);
v[s[0]] = { x,y };
}
for (int i = 'A'; i <= 'Z'; i++) v1[i] = v[i];
string s;
while (cin >> s) cout << cl(s) << endl;
return 0;
}

【例题 6-3 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. stack UVA 442 Matrix Chain Multiplication

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

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

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

  5. UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)

    题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的 ...

  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[HDU1082]

    Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. UVa442 Matrix Chain Multiplication

    // UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...

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

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

随机推荐

  1. .Net 扩展的使用

    Product.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; name ...

  2. js -- fileData 实现文件断点续传

    前端实现文件的断点续传 一.一些知识准备 断点续传,既然有断,那就应该有文件分割的过程,一段一段的传. 以前文件无法分割,但随着HTML5新特性的引入,类似普通字符串.数组的分割,我们可以可以使用sl ...

  3. class的写法

    java中class的写法:1.public class xxx{}2.非public类:public class A{}public class B{}必须保证一个其中一个类名是public并与ja ...

  4. 洛谷——P1314 聪明的质监员

    https://www.luogu.org/problem/show?pid=1314 题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每 ...

  5. nginx学习十一 nginx启动流程

    今天用了一天的时间看nginx的启动流程,流程还是非常复杂.基本的函数调用有十几个之多.通过看源代码和上网查资料,弄懂了一些函数.有些函数还在学习中,有些函数还待日后学习,这里记录一下今天所学.加油! ...

  6. Android广告轮播图实现

    先看效果 第一步,布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmln ...

  7. C语言的多行宏定义

    一.多行宏定义的使用 最近在跟STM32L011K4T6低功耗的源代码,发现使用了多行的宏定义来封装函数,记得之前把\给删除掉,编译程序就一直报错. \是续行操作符,也就是宏定义一行写不完,需要多行写 ...

  8. [Vue + TS] Create your own Decorators in Vue with TypeScript

    We’ve used @Watch, @Inject and more decorators from vue-property-decorator. In this lesson however w ...

  9. ORA-00957: 反复的列名

    1.错误描写叙述 ORA-00957: 反复的列名 2.错误原因 SQL> create table info( 2 stu_id varchar2(7) not null, 3 stu_nam ...

  10. textview-调节字体间距跟行距

    直接进行代码验证 1)当没有添加字体间距属性的时候 <TextView android:id="@+id/text_view" android:layout_width=&q ...