hdu-1082Matrix Chain Multiplication

栈的简单使用

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android是一种基于Linux内核(不包含GNU组件)的自由及开放源代码的移动操作系统,主要应用于移动设备,如智能手机和平板电脑。该系统最初由安迪·鲁宾开发,后被Google公司收购并注资,随后与多家硬件制造商、软件开发商及电信营运商共同研发改良。 Android操作系统的特点包括: 开放源代码:Android系统采用开放源代码模式,允许开发者自由访问、修改和定制操作系统,这促进了技术的创新和发展,使得Android系统具有高度的灵活性和可定制性。 多任务处理:Android允许用户同时运行多个应用程序,并且可以轻松地在不同应用程序之间切换,提高了效率和便利性。 丰富的应用生态系统:Android系统拥有庞大的应用程序生态系统,用户可以从Google Play商店或其他第三方应用市场下载和安装各种各样的应用程序,满足各种需求。 可定制性:Android操作系统可以根据用户的个人喜好进行定制,用户可以更改主题、小部件和图标等,以使其界面更符合个人风格和偏好。 多种设备支持:Android操作系统可以运行在多种不同类型的设备上,包括手机、平板电脑、智能电视、汽车导航系统等。 此外,Android系统还有一些常见的问题,如应用崩溃、电池耗电过快、Wi-Fi连接问题、存储空间不足、更新问题等。针对这些问题,用户可以尝试一些基本的解决方法,如清除应用缓存和数据、降低屏幕亮度、关闭没有使用的连接和传感器、限制后台运行的应用、删除不需要的文件和应用等。 随着Android系统的不断发展,其功能和性能也在不断提升。例如,最新的Android版本引入了更多的安全性和隐私保护功能,以及更流畅的用户界面和更强大的性能。此外,Android系统也在不断探索新的应用场景,如智能家居、虚拟现实、人工智能等领域。 总之,Android系统是一种功能强大、灵活可定制、拥有丰富应用生态系统的移动操作系统,在全球范围内拥有广泛的用户基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值