Question: Enter the number of rows and columns in a matrix of no more than 26, and then ask some expressions to find the number of basic operations required for each expression.
--> Use STL to open two stacks, one for "(", one for storing matrices, and one for scanning expressions. When a matrix is encountered, it is placed in the matrix stack; when "(" is met, it is placed in the symbol stack. When ")" is met, two matrices are multiplied from the matrix stack, and the multiplication times and are recorded, put the matrix after multiplication into the matrix stack, delete one (from the symbol stack, and output the result.
[Cpp]
# Include <iostream>
# Include <stack>
Using namespace std;
Struct Matrix // Matrix type, including the number of rows and columns
{
Int row; // number of rows
Int col; // Number of Columns
};
Int main ()
{
Int n, I; // n indicates the number of input matrices.
Matrix mat [30]; // used to store each single Matrix (up to 26, so the space for 30 is available !!!)
Char c;
Cin> n;
For (I = 0; I <n; I ++)
{
Cin> c;
Int index = (int) (c-'A'); // Let the subscript 0, 1, 2... of the matrix array mark A, B, C...
Cin> mat [index]. row> mat [index]. col; // stores the corresponding number of rows and number of columns.
}
String s; // input each row of test data in the form of a string
While (cin> s)
{
Long sum = 0; // sum of the basic multiplication numbers, initially 0
Int len = s. length ();
Stack <char> st_sign; // This stack is used to store "("
Stack <Matrix> st_node; // This stack is used to store "Matrices"
Int OK = 1; // OK indicates whether an error occurs.
For (I = 0; I <len; I ++) // Scan each character
If (s [I] = ') // when ")" appears, "(" corresponds
{
Int right_value = st_node.top (). col; // retrieves the matrix data from the stack. The number of columns in the right Matrix
Int mid_value_right = st_node.top (). row; // number of rows in the right Matrix
St_node.pop ();
Int left_value = st_node.top (). row; // retrieves the matrix data from the stack. The number of rows in the left Matrix
Int mid_value_left = st_node.top (). col; // Number of columns in the left Matrix
If (mid_value_left! = Mid_value_right) // The number of columns in the left matrix! = When the number of rows in the right matrix is
{
OK = 0;
Break;
}
St_node.pop ();
St_sign.pop ();
Sum + = left_value * mid_value_left * right_value; // accumulate the basic multiplication times.
Matrix newmat;
Newmat. row = left_value;
Newmat. col = right_value;
St_node.push (newmat); // create a matrix. The number of rows is the number of rows in the left matrix, and the number of columns is the number of columns in the right matrix.
}
Else if (s [I] = '(') // hit "(" put it directly into the symbol stack.
St_sign.push ('(');
Else // when the letter is scanned as the matrix
{
Int new_index = (int) (s [I]-'A'); // convert subscript
St_node.push (mat [new_index]); // puts the matrix into the matrix Stack
}
If (OK)
Cout <sum <endl;
Else
Cout <"error" <endl;
}
Return 0;
}