关闭

hdu-1082Matrix Chain Multiplication

428人阅读 评论(0) 收藏 举报
分类:

栈的简单使用

Matrix Chain Multiplication

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 930    Accepted Submission(s): 634


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
 

题目大意:这是一个关于矩阵的运算的题。要求表达式最后所有的矩阵运算中乘法的运算次数的总和。

解题思路:用栈,把表达式压入栈中,然后遇见“)”便弹出两个矩阵进行计算,并求出乘法运算的次数。(第一次亲自动手切掉一个关于栈的题目,而且还是1A,信心顿时提升十个百分点大笑)c++的STL库还是挺好用的.......

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<iostream>
#include<algorithm>
#define maxn 1100
#define LL __int64
using namespace std;

struct Matrix//用结构体存放矩阵的信息
{
    char c;
    LL row,col;
}m[30];

stack<Matrix> s;
char str[maxn];
int main()
{
    int t,i,j;
    LL sum;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        getchar();
        scanf("%c%I64d%I64d",&m[i].c,&m[i].row,&m[i].col);
    }
    getchar();
    while(gets(str)!=NULL)
    {
        sum=0;
        int len=strlen(str);
        int flag=0;
        for(i=0;i<len;i++)
        {
            if(str[i]!='('&&str[i]!=')')//对输入的矩阵进行判断
            {
                for(j=0;j<t;j++)
                {
                    if(str[i]==m[j].c)//如果表达式中的矩阵是已知信息的矩阵,就把它压入栈中
                    {
                        s.push(m[j]);
                        break;
                    }
                }
                if(j==t)//说明表达式中的矩阵是不可知的
                    flag=1;
            }
            if(str[i]==')')//遇见一个右括号就弹出两个矩阵进行运算
            {
                Matrix temp1,temp2,temp;
                temp1=s.top();
                s.pop();
                temp2=s.top();
                s.pop();
                if(temp1.row==temp2.col)
                {
                    temp.row=temp2.row;
                    temp.col=temp1.col;
                    sum+=temp.row*temp.col*temp1.row;//求出这次运算中进行的乘法运算的次数
                    s.push(temp);//两个矩阵运算完之后会得到一个新的矩阵,保存它的信息,并且压入栈中
                }
                else flag=2;//说明这两个矩阵不能进行运算
            }
            if(flag)
                break;
        }
        if(flag)
            printf("error\n");
        else
            printf("%I64d\n",sum);
    }
    return 0;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44470次
    • 积分:1302
    • 等级:
    • 排名:千里之外
    • 原创:85篇
    • 转载:7篇
    • 译文:0篇
    • 评论:13条
    最新评论