zoukankan      html  css  js  c++  java
  • UVa 442-矩阵链乘 Matrix Chain Multiplication

    题目描述

    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.定义结构体数组,存储矩阵的行列。如,
    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
    

    2.定义栈,存放要计算的输入的数据。(括号无需入栈)如,
    (AA)
    (AB)
    (AC)
    (A(BC))
    ((AB)C)
    (((((DE)F)G)H)I)
    (D(E(F(G(HI)))))
    ((D(EF))((GH)I))

    3.逐个分析要计算的输入字符串,遇到‘)’,出栈并计算把结果存入栈。
    代码
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<stack>
     4 #include<string>
     5 using namespace std;
     6 struct mat{
     7         int a,b;
     8         mat(int a=0,int b=0):a(a),b(b){}
     9     } m[26];
    10     stack<mat>s;
    11 int main()
    12 {
    13     int n;
    14     cin>>n;
    15     for(int i=0;i<n;i++)
    16     {
    17         char q;
    18         cin>>q;
    19         int k=q-'A';
    20         cin>>m[k].a>>m[k].b;
    21     }
    22     string z;
    23     while(cin>>z)
    24     {
    25         int len=z.length();
    26         bool error=0;
    27         int ans=0;
    28         for(int i=0;i<len;i++)
    29         {
    30             if(isalpha(z[i])) s.push(m[z[i]-'A']);
    31             else if(z[i]==')')
    32             {
    33                 mat m2=s.top();s.pop();
    34                 mat m1=s.top();s.pop();
    35                 if(m1.b!=m2.a){
    36                     error=1; break;
    37                 }
    38                 ans+=m1.a*m1.b*m2.b;
    39                 s.push(mat(m1.a,m2.b));
    40             }    
    41         }
    42         if(error) cout<<"error"<<endl;
    43         else cout<<ans<<endl;
    44     }
    45     return 0;
    46  } 
  • 相关阅读:
    ORM
    优酷:exec 补充,元类,优酷构架,ORM。
    数据库 事务。
    Python操作mysql
    数据库对表操作的练习题总结。
    ACM-ICPC 2018 南京赛区网络预赛 E. AC Challenge (状压DP)
    CodeForces
    CodeForces
    AcWing 314. 低买 (线性DP)打卡
    AcWing 313. 花店橱窗 (线性DP)打卡
  • 原文地址:https://www.cnblogs.com/masking-timeflows/p/6767268.html
Copyright © 2011-2022 走看看