zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    windows常用快捷键
    【Linux】查看系统位数
    【Centos】yum 安装mariaDB
    【Centos7 GRUB】修改开机等待时间
    Shell脚本编写规范
    【Shell Basic】source . 与 bash sh 的区别
    linux防火墙之 ufw
    【HotSpot】jps命令行详解
    【Ubuntu 16】网络配置文件
    【刷题】BZOJ 2179 FFT快速傅立叶
  • 原文地址:https://www.cnblogs.com/zhaopeng938/p/9420390.html
Copyright © 2011-2022 走看看