stack UVA 442 Matrix Chain Multiplication

题目传送门

/*
    stack 容器的应用:矩阵的表达式求值
    A 矩阵是a * b,B 矩阵是b * c,则A * B 是a * c
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;

const int MAXN = 1e4 + 10;
const int INF = 0x3f3f3f3f;

struct Matrix
{
    int a, b;
    Matrix (int a=0, int b=0) : a (a), b (b) {};
}m[26];

int main(void)        //UVA 442 Matrix Chain Multiplication
{
    //freopen ("UVA_446.in", "r", stdin);

    int n;
    string name;

    cin >> n;
    for (int i=1; i<=n; ++i)
    {
        cin >> name;
        int k = name[0] - ‘A‘;
        cin >> m[k].a >> m[k].b;
    }

    string expr;
    stack <Matrix> s;
    while (cin >> expr)
    {
        int len = expr.length ();
        bool error = false;
        int ans = 0;

        for (int i=0; i<len; ++i)
        {
            if (expr[i] >= ‘A‘ && expr[i] <= ‘Z‘)    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)    cout << "error" << endl;
        else    cout << ans << endl;
    }

    return 0;
}
时间: 2024-10-09 13:59:16

stack UVA 442 Matrix Chain Multiplication的相关文章

[2016-02-05][UVA][442][Matrix Chain Multiplication]

[2016-02-05][UVA][442][Matrix Chain Multiplication] UVA - 442 Matrix Chain Multiplication Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Suppose you have to evaluate an expression like A*B*C*D*E where

UVa 442 Matrix Chain Multiplication(矩阵链乘,模拟栈)

题意  计算给定矩阵链乘表达式需要计算的次数  当前一个矩阵的列数等于后一个矩阵的行数时  他们才可以相乘  不合法输出error 输入是严格合法的  即使只有两个相乘也会用括号括起来  而且括号里最多有两个 那么就很简单了 遇到字母直接入栈  遇到反括号计算后入栈  然后就得到结果了 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int N = 1000;

UVA - 442 Matrix Chain Multiplication

点击打开链接 题目意思是求矩阵相乘的运算次数, 设A size为n*s,B size为s*m 那么A*B运算量为n*m*s. 注意括号里面的优先级,并且依次累加即可,并且没有不合法的序列. 思路是先对输入的n个矩阵编号按照字典序排序,因为每次两个矩阵相乘会得到一个新的矩阵,编号可以设置成在n的编号加1,并且重新压入栈中. #include <iostream> #include <cstdio> #include <cmath> #include <vector&

UVA - 442 Matrix Chain Multiplication 双端队列

题目大意:给出n个矩阵和表达式,问该表达式是否正确,如果计算正确,输出计算了多少次 解题思路:双端队列,遇到右括号时弹出后面的两个矩阵进行乘法,相乘时要注意顺序,是第二个出队列的乘上第一个出队列的. #include<cstdio> #include<algorithm> #include<deque> #include<map> #include<cstring> using namespace std; #define maxn 50010

UVa 442 Matrix Chain Multiplication(栈的应用)

题目链接: https://cn.vjudge.net/problem/UVA-442 1 /* 2 问题 3 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 4 5 解题思路 6 栈的应用,直接忽视左括号,每次只计算栈顶的两个矩阵会更加方便. 7 */ 8 #include<cstdio> 9 #include<cstring> 10 #include<stack> 11 #include<cctype> 12 using namespac

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

题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的次数,累加就可以了. PS:注意弹出的矩阵表示的先后顺序. 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1000000000 #define mod 1000000007 #define FRE() freopen

442 - Matrix Chain Multiplication

Matrix Chain Multiplication Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the number of

UVA 442 二十 Matrix Chain Multiplication

Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 442 Appoint description:  System Crawler  (2015-08-25) Description Suppose you have to evaluate an expression like A*B*C*D*E

UVA Matrix Chain Multiplication

题目如下: Matrix Chain Multiplication Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the numb