zoukankan      html  css  js  c++  java
  • UVA 442 二十 Matrix Chain Multiplication

    Matrix Chain Multiplication
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
    Appoint description: 

    Description

    Download as PDF
     

    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 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 ( tex2html_wrap_inline28 ), 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 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
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stack>
     4 #include <iostream>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 struct Node
     9 {
    10     int a;
    11     int b;
    12 };
    13 
    14 stack <Node> s;
    15 
    16 int main()
    17 {
    18     Node m[26];
    19     int n;
    20     int i,j,k,flg;
    21     char nu[10],str[1000];
    22     while(scanf("%d",&n)!=EOF)
    23     {
    24         for(i=1;i<=n;i++)
    25         {
    26             scanf("%s",nu);
    27             k=nu[0]-'A';
    28             scanf("%d %d",&m[k].a,&m[k].b);
    29         }
    30 
    31         while(scanf("%s",str)!=EOF)
    32         {
    33             if(!s.empty())
    34                 s.pop();
    35             int l=strlen(str),final=0;;
    36             flg=0;
    37             for(i=0;i<l;i++)
    38             {
    39                 if(isalpha(str[i]))
    40                     s.push(m[str[i]-'A']);
    41                 else if(str[i]==')')
    42                 {
    43                     Node m1,m2,mm;
    44                     m2=s.top();s.pop();
    45                     m1=s.top();s.pop();
    46                     if(m1.b!=m2.a)
    47                     {
    48                         flg=1;
    49                         break;
    50                     }
    51                     final=final+m1.b*m1.a*m2.b;
    52                     mm.a=m1.a,mm.b=m2.b;
    53                     s.push(mm);
    54                 }
    55             }
    56             if(flg==1)
    57                 printf("error
    ");
    58             else
    59                 printf("%d
    ",final);
    60         }
    61     }
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    java第九周上机
    安卓-计算器
    安卓第四周作业
    第十五周作业
    第十三周作业
    第十三周上机作业
    第十二周作业
    第十二周上机作业
    第十一周作业
    第十一周上机作业
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771620.html
Copyright © 2011-2022 走看看