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

【题意】

在这里输入题意

【题解】

用栈来处理一下表达式就好。
因为括号是一定匹配的。所以简单很多。
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. php中str_repeat函数

    php中str_repeat函数 一.作用 用于repeat str 二.实例:输出菱形 代码: <!DOCTYPE html> <html lang="en"& ...

  2. vue和miniui 一起使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 【习题 8-10 UVA - 1614】Hell on the Markets

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 证明:前i个数一定能凑够1..sum[i]中的所有数字 i=1时显然成立. 现在假设i>=2时结论成立 即前i个数字能凑出1. ...

  4. 将NSTimer加入至RunLoop中的两种方法差别

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  5. 保留的 IPv4 地址

    保留的IP地址   https://en.wikipedia.org/wiki/Reserved_IP_addresses 地址块(CIDR) 范围 地址数量 范围 目的 0.0.0.0/8 0.0. ...

  6. eclipse - 下载网址

    这里面有着非常齐全的eclipse相关资源,而且都是放在网盘里面的,下载也方便 http://www.androiddevtools.cn/

  7. C++ 为什么要virtual析构函数

    class A { public: A() { printf("A()\n"); } virtual ~A() { printf("~A()\n"); } }; ...

  8. mysql 批量删除数据

    批量删除2000w数据 使用delete from table太慢 //DELIMITER DROP PROCEDURE if EXISTS deleteManyTable; create PROCE ...

  9. 至顶网推荐-Rpm另类用法加固Linux安全

    http://www.zdnet.com.cn/ 650) this.width=650;" onclick='window.open("http://blog.51cto.com ...

  10. Mysql数据库的瓶颈处理一点建议

    Mysql数据库的瓶颈处理一点建议         我们在使用Mysql数据库是常见的两个瓶颈是CPU和I/O的瓶颈,CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据时候.磁盘I/O瓶颈的出 ...