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>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
//#include <stack>
#include <map>
#include <queue>

#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define N 100005
using namespace std;

struct node
{
    int a,b;
    char c;
}p[30];

bool cmp(node x,node y)
{
    return x.c<y.c;
}

char s[10000],stack[10000]; //栈 

int main()
{
    //Read();
    int n,i,j,k;
    while(~scanf("%d",&n))
    {
        getchar();
        for(i=0;i<n;i++)
        {
            scanf("%c %d %d",&p[i].c,&p[i].a,&p[i].b);
            getchar();
        }
        sort(p,p+n,cmp);
        while(gets(s))
        {
            int l=strlen(s),top,num=0;
            for(i=0,top=0;i<l;i++)
            {
                if(s[i]=='(') stack[top++]=s[i];
                else if(s[i]>='A'&&s[i]<='Z')  stack[top++]=s[i]; //把不是 右括号的都压入栈
                else if(s[i]==')')
                {
                    for(j=0;j<n;j++) if(stack[top-2]==p[j].c) break;
                    for(k=0;k<n;k++) if(stack[top-1]==p[k].c) break; //找出 编号
                    if(p[j].b!=p[k].a) break; //如果矩阵不能相乘  就跳出
                    num+=p[j].a*p[j].b*p[k].b;
                   // printf("%d\n",num);
                    p[n].a=p[j].a;p[n].b=p[k].b;p[n].c=p[n-1].c+1; //生成一个新的矩阵
                    stack[top-3]=p[n].c;top-=2; //退栈
                    n++;
                }
            }
            if(i<l) printf("error\n");
            else printf("%d\n",num);
        }
    }
    return 0;
}
时间: 2024-08-07 03:50:52

UVA - 442 Matrix Chain Multiplication的相关文章

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 namespac

[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 双端队列

题目大意:给出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