Data Structure topic of the data structure of the ultraviolet A 442-matrix chain Multiplication

Source: Internet
Author: User
442-Matrix
Chain Multiplication
5134 59.82% 2559 92.93%

Question Link: Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 103 & page = show_problem & problem = 383

Question type: data structure, linked list

Sample input:

9A 50 10B 10 20C 20 5D 30 35E 35 15F 15 5G 5 10H 10 20I 20 25ABC(AA)(AB)(AC)(A(BC))((AB)C)(((((DE)F)G)H)I)(D(E(F(G(HI)))))((D(EF))((GH)I))

Sample output:

000error10000error350015000405004750015125


Question:

Give them a series of matrices named a, B ...... And their number of rows and columns. After the expression is given, a series of expressions are required to find the number of multiplication steps for calculation based on these expressions.


Disintegration ideas:

This question matches the question with the brackets. The key step is to calculate the number of matrix multiplications.

# Include <cstdio> # include <cctype> # include <cstring> # include <cstdlib> # include <stack> using namespace STD; int sum, N; int mat [30] [2]; int arr [30], Prev [30], next [30]; char STR [1000]; void solve () {// if there is only one matrix, the result 0 if (strlen (STR) = 1 & isupper (STR [0]) is directly output. {printf ("0 \ n") ;}else {char copymat [30] [2]; int I, j; // copy an array for operation. Because the subsequent operations need to modify these matrices for (I = 0; I <n; ++ I) {copymat [I] [0] = mat [I] [0]; copymat [I] [1] = mat [I] [1];} sum = 0; stack <char> st; for (I = 0; I <strlen (STR); ++ I) {If (STR [I] = '(' | isupper (STR [I]) ST. push (STR [I]); else if (STR [I] = ') {stack <char> temp; // when') 'is encountered, calculate all the matrices in the stack while (isupper (St. top () {temp. push (St. top (); ST. pop ();} // put '(' Also pops up St. pop (); char ex; // retrieve the first Matrix (the leftmost one in the original expression) if (! Temp. Empty () {EX = temp. Top (); temp. Pop ();} while (! Temp. empty () {char multi = temp. top (); temp. pop (); // if the number of columns in the left matrix is different from the function in the right matrix, the error if (copymat [ex-'a'] [1] is directly output. = Copymat [multi-'a'] [0]) {printf ("error \ n"); return;} // calculates the number of times of multiplication, add sum + = copymat [ex-'a'] [0] * copymat [multi-'a'] [0] * copymat [multi-'a'] [1]; // obtain a new matrix after multiplication, and update the size of copymat [ex-'a'] [1] = copymat [multi-'a'] [1];} St. push (Ex) ;}} printf ("% d \ n", sum) ;}} int main () {freopen ("input.txt", "r", stdin ); char ch; int I, j; scanf ("% d % * C", & N); for (I = 0; I <n; ++ I) {scanf ("% C % d % * C", & Ch, & mat [I] [0], & mat [I] [1]);} while (gets (STR) {solve ();} return 0 ;}


-- The meaning of life is to give it meaning.

OriginalHttp://blog.csdn.net/shuangde800,
D_double



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.