zoukankan      html  css  js  c++  java
  • UVa 442 矩阵链乘(栈)

    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 (  ), 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<iostream>
     2 #include<stack>
     3 #include<string>
     4 using namespace std;
     5 
     6 struct Node
     7 {
     8     int m, n;
     9     Node(int m = 0, int n = 0) :m(m), n(n){}
    10 }Matrix[30];
    11 
    12 stack<Node> chain;
    13 
    14 void clear()
    15 {
    16     while (!chain.empty())
    17         chain.pop();
    18 }
    19 
    20 void Multiplication(string s)
    21 {   
    22     clear();
    23     int count = 0;
    24     if (s[0] != '(')    {cout << "0" << endl; return;}
    25     for (int i = 0; i < s.length(); i++)
    26     {
    27         if (s[i] == ')')
    28         {
    29             Node x2 = chain.top();
    30             chain.pop();
    31             Node x1 = chain.top();
    32             chain.pop();
    33             if (x1.n != x2.m)  { cout << "error" << endl; return; }
    34             else
    35             {
    36                 count += x1.m * x1.n * x2.n;
    37                 chain.push(Node(x1.m,x2.n));
    38             }
    39         }
    40         else if (s[i] == '(')  { ; }
    41         else  chain.push(Matrix[s[i]-'A']);
    42     }
    43     cout << count << endl;
    44 }
    45 
    46 int main()
    47 {
    48     int t, m, n;
    49     char name;
    50     cin >> t;
    51     while (t--)
    52     {
    53         cin >> name >> m >> n;
    54         Matrix[name - 'A'].m = m;
    55         Matrix[name - 'A'].n = n;
    56     }
    57     string s;
    58     while (cin >> s)
    59     {
    60         Multiplication(s);
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    docker mysql 主从配置
    在docker上安装运行mysql实例
    mongodb分片集搭建
    mongodb片健的选取及更改
    MySQL 5.7的多源复制
    percona-toolkit使用教程
    Python基础操作-集合
    nginx location 在配置中的优先级
    OpenResty知识汇集
    开源分布式日志框架
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/6107159.html
Copyright © 2011-2022 走看看