• HDU 1082 Matrix Chain Multiplication


    Matrix Chain Multiplication

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


    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
     
     
    矩阵相乘
     1 #include<iostream>
     2 #include<string>
     3 //#include<fstream>
     4 #include<stack>
     5 using namespace std;
     6 struct pos
     7 {
     8     char ch;
     9     int row, col;
    10 };
    11 int main()
    12 {
    13     //ifstream in("data.txt");
    14     int n, rows[26]={0}, cols[26]={0};
    15     cin>>n;
    16     while(n--)
    17     {
    18         char ch;
    19         int row, col;
    20         cin>>ch>>row>>col;
    21         rows[ch-65]=row;
    22         cols[ch-65]=col;
    23     }
    24     string s;
    25     stack<pos> sta;
    26     while(cin>>s)
    27     {
    28         int sum=0;
    29         pos temp,temp1;
    30         for(int i=0;i<s.size();i++)
    31         {
    32             if(s[i]==')')
    33             {
    34                 temp=sta.top();sta.pop();
    35                 temp1=sta.top();sta.pop();
    36                 sta.pop();
    37                 if(temp1.col!=temp.row)
    38                 {
    39                     sum=-1;
    40                     break;
    41                 }
    42                 else
    43                 {
    44                     sum+=temp1.row*temp1.col*temp.col;
    45                     temp.ch='*';
    46                     temp.row=temp1.row;
    47                     sta.push(temp);
    48                 }
    49             }
    50             else if(s[i]=='(')
    51             {
    52                 temp.ch='(';
    53                 temp.row=0;
    54                 temp.col=0;
    55                 sta.push(temp);
    56             }
    57             else
    58             {
    59                 temp.ch='*';
    60                 temp.row=rows[s[i]-65];
    61                 temp.col=cols[s[i]-65];
    62                 sta.push(temp);
    63             }
    64         }
    65         if(sum==-1)
    66             cout<<"error"<<endl;
    67         else
    68             cout<<sum<<endl;
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    艺术与科技的狂欢,云端XR支撑阿那亚2022砂之盒沉浸艺术季
    阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
    圣庭医疗联合创始人谷红仓:高通量基因测序在药物研发和靶向治疗中的应用
    阿里云贾朝辉:云 XR 平台支持彼真科技呈现国风科幻虚拟演唱会
    阿里云鲍文乐:基于事件的自动化运维最佳实践
    加码企业服务,阿里云发布计算巢加速器
    阿里云张新涛:支持沉浸式体验应用快速落地,阿里云云XR平台发布
    阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
    阿里云杨红军:应用管理——云上资源DevOps最佳实践
    最多可省19%!阿里云第七代云服务器ECS中国大陆地域调价通知
  • 原文地址:https://www.cnblogs.com/zhaopeng938/p/9420390.html
Copyright © 2020-2023  润新知