hdu 1082 Matrix Chain Multiplication

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

题意:

  给定n个矩阵,让你用矩阵乘法进行表达式求值。例如:  

 A*B=A.y*A.y*B.y,(如果A.y!=B.x就直接输出error)。

(A(BC))=A*(B*C)=b.x*b.y*c.y+a.x*a.y*c.y=10*20*5+50*10*5=3500

(B*C计算完结果后,得出一个新的矩阵m,m.x=b.x,m.y=c.y,A*M=a.x*a.y*m.y=a.x*a.y*c.y,最后再相加)。

思路:

建立一个结构体,用来存矩阵。从最中间的括号开始,用栈来存到达最里面括号之前的矩阵,当遇到第一个‘)’时,弹出2个矩阵,使‘)’符号前面的两个数相乘并记录,同时存入他们计算后得到的新的矩阵。

代码:

#include<iostream>
#include<stack>
using namespace std;
struct Matrix
{
    int x,y;
}m[32];
int main()
{
    int n;
    char k;
    string s;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>k;
        cin>>m[k-'A'].x>>m[k-'A'].y;
    }
    while(cin>>s)
    {
        int sum=0,flag=1;
        stack<Matrix>e;
        for(int i=0;i<s.size()&&flag;i++)
        {
            if(s[i]=='(')
                continue;
            else if(s[i]==')')
            {
                Matrix aa=e.top();
                e.pop();
                Matrix bb=e.top();
                e.pop();
                if(aa.x!=bb.y)
                {
                    flag=0;
                    break;
                }
                else
                {
                    sum+=aa.y*bb.x*bb.y;
                    Matrix r;
                    r.x=bb.x,r.y=aa.y;
                    e.push(r);
                }
            }
            else
                e.push(m[s[i]-'A']);
        }
        if(flag)
            cout<<sum<<endl;
        else
            cout<<"error"<<endl;
    }
    return 0;
}

阅读更多

Matrix Chain Multiplication

10-24

escriptionnSuppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. nSince 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. nFor example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix. nThere are two different strategies to compute A*B*C, namely (A*B)*C and A*(B*C). nThe first one takes 15000 elementary multiplications, but the second one only 3500. nnYour job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy. nInputnInput consists of two parts: a list of matrices and a list of expressions. nThe 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. nThe second part of the input file strictly adheres to the following syntax (given in EBNF): nSecondPart = Line Line nnLine = Expression nnExpression = Matrix | "(" Expression Expression ")"nnMatrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"nOutputnFor 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.nSample Inputn9nA 50 10nB 10 20nC 20 5nD 30 35nE 35 15nF 15 5nG 5 10nH 10 20nI 20 25nAnBnCn(AA)n(AB)n(AC)n(A(BC))n((AB)C)n(((((DE)F)G)H)I)n(D(E(F(G(HI)))))n((D(EF))((GH)I))nSample Outputn0n0n0nerrorn10000nerrorn3500n15000n40500n47500n15125

没有更多推荐了,返回首页