FZU1061

 Problem Description   link

Given n matrices {A1, A2, ..., An}, examine the continuous product A1A2 ... An of these n matrices. Since matrix multiplication satisfies the combination law, there can be many different calculation orders for calculating the continuous product of matrices.

The calculation order of matrix continuous product is closely related to its calculation amount. For example, consider the example of calculating the continuous product of three matrices {A1, A2, A3}. Let the dimensions of these three matrices be 10 * 100, 100 * 5, and 5 * 50, respectively. If calculated according to (A1A2) A3, the number of multiplication times required for the product of 3 consecutive matrices is 10 * 100 * 5 + 10 * 5 * 50 = 7500. If you calculate according to A1 (A2A3), you need 100 * 5 * 50 + 10 * 100 * 50 = 75000 times.

Now your task is to calculate the required number of times for a certain matrix multiplication scheme.

 Input

The input data consists of multiple sets of data. The format of each group of data is as follows: The
first row is an integer n (1≤n≤26), indicating the number of matrices.
In the next n rows, each row has an uppercase letter, indicating the name of the matrix, followed by two integers a, b, respectively representing the number of rows and columns of the matrix, where 1 <a, b <100.
Line n + 1 is an expression of matrix multiplication, consisting of parentheses and capital letters, without multiplication signs and extra spaces. If there are no parentheses in the expression, they are calculated from left to right, and the entered parentheses are guaranteed to match.

 Output

For each set of data, only one row of the output contains an integer, that is, the number of times the matrix multiplication scheme requires. If the matrix multiplication rule is not met during the operation (that is, the number of columns in the left matrix is ​​different from the number of rows in the right matrix), then "error" is output.

 Sample Input

3 A 10 100 B 100 5 C 5 50 A(BC)

 Sample Output

75000
#include <iostream> 
#include < string > 
#include <deque>
 using  namespace std; 

/ * 
    Idea: do it with STL double-ended queue, and process it in two rounds. The 
    first round: first process the inner sequence of brackets, because the brackets may be considered Superimpose (()), so every time you encounter), find out the sequence in a bracket and calculate it, and put the result back in the sequence. The 
    second round: because the brackets are processed, you can 
    pay attention to it if you multiply it by left : if it is not The row and column of the matrix can be jumped out 
* / 

// Matrix 
struct Matrix 
{ 
    int row, lie; 
}; 

int main () 
{ 
    Matrix ms [ 28 ]; 

    int n, row, lie;
     char name;
     string xl; // Store the input sequence 
    while(CIN >> n-) 
    { 
        the while (N-- ) 
        { 
            CIN >> name >> >> Row Lie; 
            MS [name - ' A ' ] .Row = Row; 
            MS [name - ' A ' ] = .lie Lie ; 
        } 
        getchar (); 
        // Get sequence 
        getline (cin, xl);
         long  long sum = 0 ; 
        deque < char > dq;
         // First round of filter brackets 
        for (int i = 0 ; i <xl.size (); i ++ ) 
        { 
            if (xl [i]! = ' ) ' ) 
            { 
                dq.push_back (xl [i]); 
            } 
            // Because the parentheses meet the condition 
            else 
            { 
                string s ;
                 while (dq.back ()! = ' ( ' ) 
                { 
                    s + = dq.back (); 
                    dq.pop_back (); 
                } 
                dq.pop_back (); 

                // If (ABC) is out of the queue, it is CBA
                 //Then multiply ABC 
                for ( int j = s.size () -1 ; j> 0 ; j-- ) 
                { 
                    int m1 = s [j]- ' A ' ;      // The first matrix 
                    int m2 = s [j- 1 ]- ' A ' ; // Second matrix 
                    if (ms [m1] .lie! = ms [m2] .row) 
                    { 
                        // Does not conform to the matrix rule mark 
                        sum = -1 ;
                         break ; 
                    } 
                    sum+ = ms [m1] .row * ms [m1] .lie * ms [m2] .lie;
                     // The new matrix is ​​stored in the second matrix and iterated 
                    ms [m2] .row = ms [m1] .row; 
                } 
                // The multiplied matrix is ​​put back into the sequence 
                dq.push_back (s [ 0 ]);
                 if (sum ==- 1 )
                     break ; 
            } 
        } 
        if (sum! = -1 ) 
        { 
            // Process normal sequence 
            string s;
             while (dq.size ()) 
            { 
                s + = dq.front ();
                dq.pop_front();
            }
            for (int j = 0; j < s.size() - 1; j++)
            {
                int m1 = s[j] - 'A';
                int m2 = s[j + 1] - 'A';
                if (ms[m1].lie != ms[m2].row)
                {
                    sum = -1;
                    break;
                }
                sum += ms[m1].row * ms[m1].lie * ms[m2].lie;
                ms[m2].row = ms[m1].row;
            }
            if (sum != -1)
            {
                cout << sum << endl;
                continue;
            }
        }
        cout << "error" << endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/dlvguo/p/12680238.html