HDU 1082 Matrix Chain Multiplication 逆波兰式

本文介绍了一种解决矩阵乘法步骤计算问题的方法,通过将中缀表达式转化为逆波兰表达式(后缀表达式),并提供了一种巧妙的转换技巧,即用‘*’替换’)’,然后倒序计算逆波兰表达式。文章还提供了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:计算给出的表达式需要多少步?
思路:矩阵乘法 [n,m][m,k]=[n,k] ,需要 nmk 步。把中缀表达式转成后缀表达式,这个题目的转换有一种讨巧的方法,直接把’)’换成’*’即可,注意需要把这个式子反一下来计算。然后来解这个逆波兰式即可。解逆波兰式的原则,如果碰到的符号,就用这个符号计算栈顶的两个数,然后把结果入栈。如果碰到数字,直接入栈。

http://acm.hdu.edu.cn/showproblem.php?pid=1082

/*********************************************
    Problem : HDU 1082
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++)
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m,k;

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};

struct Node {
    int x,y;//x行y列
    Node(){}
    Node(int _x,int _y) {x = _x ; y = _y;}
}A[30];

stack<char>s1,s2;
stack<Node>ANS;

void input() {
    scanf("%d",&n);
    char tmpc[10];
    int tmpx,tmpy;
    rep(i,1,n) {
        scanf("%s %d %d",tmpc,&tmpx,&tmpy);
        A[tmpc[0]-'A'].x = tmpx;
        A[tmpc[0]-'A'].y = tmpy;
    }
}

char a[1005];

void solve() {
    while(~scanf("%s",a)) {
        while(!s1.empty())s1.pop();
        while(!s2.empty())s2.pop();
        while(!ANS.empty())ANS.pop();
        int lena = strlen(a);
        rep(i,0,lena-1) {
            if(a[i]==')') {
                s1.push('*');
            }
            else if(a[i] >= 'A' && a[i] <= 'Z'){
                s1.push(a[i]);
            }
        }
        while(!s1.empty()) {
            s2.push(s1.top());
            s1.pop();
        }
        int ans = 0;
        int err = 0;
        while(!s2.empty()) {
            char tmpc = s2.top();
            s2.pop();
            if(tmpc >= 'A' && tmpc <= 'Z') {
                ANS.push(A[tmpc-'A']);
            }
            else {
                Node tmp2 = ANS.top();ANS.pop();
                Node tmp1 = ANS.top();ANS.pop();
                if(tmp1.y != tmp2.x) { err = 1 ; break ; }
                Node tmp = Node(tmp1.x,tmp2.y);
                ans += tmp1.x * tmp1.y * tmp2.y;
                ANS.push(tmp);
            }
        }
        if(err) {
            puts("error");
        }
        else {
            printf("%d\n",ans);
        }
    }
}

int main(void) {
        input();
        solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值