UV 442 Matrix Chain Multiplication——思路题

网友投稿 204 2022-11-27

UV 442 Matrix Chain Multiplication——思路题

#include #include #include #include #include using namespace std;struct matrix{ int a, b; matrix(int x = 0, int y = 0){//构造函数初始化a、b a = x, b = y; }}m[26];int main(){ stack s; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { char ch; getchar();//每次都接收一个换行 ch = getchar(); int temp = ch - 'A'; scanf("%d %d", &m[temp].a, &m[temp].b); } string expr; while (cin >> expr) { int len = expr.length(); bool error = false; int ans = 0; for (int i = 0; i < len; i++) { if (isalpha(expr[i])) s.push(m[expr[i] - 'A']); else if (expr[i] == ')') { matrix m2 = s.top(); s.pop(); matrix m1 = s.top(); s.pop(); if (m1.b != m2.a) {error = true; break;} ans += m1.a*m1.b*m2.b; s.push(matrix(m1.a, m2.b)); } } if (error) printf("error\n"); else printf("%d\n", ans); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:UVA 133 The Dole Queue——思路题
下一篇:全差分BiCMOS采样/保持电路仿真方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~