Matrix Chain Multiplication

原题:
Suppose you have to evaluate an expression like ABCDE 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 5010 matrix, B a 1020 matrix and C a 205 matrix.
There are two different strategies to compute A
BC, namely (AB)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 Specification

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 }
Line = Expression
Expression = Matrix | “(“ Expression Expression “)”
Matrix = “A” | “B” | “C” | … | “X” | “Y” | “Z”

Output Specification

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

题目大意

假设要计算A×B×C(A:50×10,B:10×20,C:20×5)
有两种顺序第一种是(A×BC,第二种是A×(B×C)
第一种要做乘法15000次,第二种要做3500次.
我们需要给定矩阵相乘的顺序,计算矩阵相乘时的所需次数.

算法分析

用map()保存矩阵参数:

map<char,Node>matrix;

其中的char是矩阵的名称,Node是矩阵行列的数据结构
用stack()模拟矩阵乘法

stack<Node>array;

遇到矩阵就进栈,遇到右括号就把栈顶两个矩阵相乘,并把结果压回栈里.

代码程序

#include <map>
#include <stack>
#include <iostream>
using namespace std;
struct Node{
    int row,col;//矩阵行列数 
};
int main()
{
    int n;                                            //矩阵个数 
    char name;                                        //矩阵名称            
    map<char,Node>matrix;                            //矩阵参数 
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>name;
        cin>>matrix[name].row>>matrix[name].col;
    }
    string exp;                                        //保存表达式 
    while(cin>>exp)
    {
        int i;
        int count=0;                                //矩阵做乘法的次数 
        stack<Node>array;                            //模拟矩阵乘法 
        for(i=0;i<exp.size();i++)
        {
            if(exp[i]=='(')continue;                //左括号跳过 
            if(exp[i]==')')                            //右括号时,将栈顶两个矩阵相乘,再压入栈 
            {
                Node a,b,t;
                b=array.top();
                array.pop();
                a=array.top();
                array.pop();
                if(a.col!=b.row)                             //不能相乘的情况 
                {
                 cout<<"error"<<endl;
                    break;
                }
                count+=a.row*a.col*b.col;                    //算次数 
                t.row=a.row;
                t.col=b.col;
                array.push(t);
            }
            else
            {
                array.push(matrix[exp[i]]);
            }

        }
        if(i==exp.size())
            cout<<count<<endl;                                //输出 
    }
    return 0;
}