HDU 1082

Matrix Chain Multiplication

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1066    Accepted Submission(s): 719

Problem Description

Matrix multiplication problem is a typical example of dynamical programming.

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 elementary multiplications needed strongly
depends on the evaluation order you choose.

For example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix.

There are two different strategies to compute A*B*C, namely (A*B)*C and A*(B*C).

The first one takes 15000 elementary multiplications, but the second one only 3500.

Your job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy.

Input

Input consists of two parts: a list of matrices and a list of expressions.

The first line of the input file contains one integer n (1 <= n <= 26), representing the number of matrices in the first part. The next n lines each contain one capital letter, specifying the name of the matrix, and two integers, specifying the number of rows
and columns of the matrix.

The second part of the input file strictly adheres to the following syntax (given in EBNF):

SecondPart = Line { Line } <EOF>

Line = Expression <CR>

Expression = Matrix | "(" Expression Expression ")"

Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"

Output

For each expression found in the second part of the input file, print one line containing the word "error" if evaluation of the expression leads to an error due to non-matching matrices. Otherwise print one line containing the number of elementary multiplications
needed to evaluate the expression in the way specified by the parentheses.

Sample Input

9

A 50 10

B 10 20

C 20 5

D 30 35

E 35 15

F 15 5

G 5 10

H 10 20

I 20 25

A

B

C

(AA)

(AB)

(AC)

(A(BC))

((AB)C)

(((((DE)F)G)H)I)

(D(E(F(G(HI)))))

((D(EF))((GH)I))

Sample Output

0

0

0

error

10000

error

3500

15000

40500

47500

15125

Source

University of Ulm Local Contest 1996

<span style="color:#330099;">/************************************************
    author    :   Grant Yuan
    time      :   2014.8.4
    algorithm :   栈的运用
    source    ;   HDU 1082
************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>

using namespace std;
char s[1000];
struct node{
  char c;
  int num1;
  int num2;
};
stack<node>q;
int n,ans;
int sta[26],fin[26];

int main()
{
     char c1;int aa,bb;
     scanf("%d",&n);
     for(int i=0;i<n;i++)
     {
         getchar();
         scanf("%c%d%d",&c1,&aa,&bb);
         sta[c1-'A']=aa;fin[c1-'A']=bb;
     }
     while(~scanf("%s",s)){
            ans=0;
            int flag=1;
        while(!q.empty()) q.pop();
        int l=strlen(s);
        for(int i=0;i<l;i++)
        {
            if(s[i]==')'){
                node n1,n2,n3;
                n1=q.top();q.pop();
                n2=q.top();q.pop();
                if(n2.num2!=n1.num1){flag=0;break;}
                ans+=n2.num1*n2.num2*n1.num2;
                q.pop();
                n3.num1=n2.num1;n3.num2=n1.num2;
                q.push(n3);
            }
            else if(s[i]>='A'&&s[i]<='Z'){
                node n1;
                n1.c=s[i];n1.num1=sta[s[i]-'A'];
                n1.num2=fin[s[i]-'A'];
                q.push(n1);
            }
            else if(s[i]=='('){
                node n1;
                n1.c=s[i];
                n1.num1=0;n1.num2=0;
                q.push(n1);
            }
        }
        if(flag==0) printf("error\n");
        else printf("%d\n",ans);
     }
     return 0;
}
</span>

HDU 1082

时间: 2024-10-12 18:49:58

HDU 1082的相关文章

HDU 1082 Matrix Chain Multiplication

评估多个矩阵乘法的基本运算次数 乍一看与分治算法有关,其实题目是个模拟矩阵相乘次数的问题,自定义类型存储矩阵,主要操作用栈实现.遇到'('继续,遇到')'算栈顶两个矩阵相乘并再放进栈顶,附代码 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <stack> 5 using namespace std; 6 struct Max { 7 int row; 8 i

HDU ACM 1082 Matrix Chain Multiplication

分析:利用栈处理.遇到矩阵时入栈:遇到")" 弹出两个矩阵进行运算,并将结果压栈.另外在矩阵相乘时注意两个矩阵是否满足相乘条件. #include<iostream> #include<stack> #include<string> #include<cctype> using namespace std; #define N 30 struct node { char m; int r,c; } a[N]; string s; bool

HDU分类

模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 12

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

HDU——PKU题目分类

HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201

HDU 1098 Ignatius&#39;s puzzle(数论-其它)

Ignatius's puzzle Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice but to appeal to Eddy. this problem describes that:f(x)=5*x^13+13*x^5+k*a*x,input a nonegative integer k(k<10000),to find the minimal

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往