Example 6-3 Matrix Chain multiplication, Uva 442

Source: Internet
Author: User

There was no problem with the idea, but it was done for nearly three hours, with 2 hours of commissioning

The experience has the following points:

    1. Must learn to debug, master the output of the intermediate amount of skills, to enhance the learning of GDB debugging
    2. Sometimes the code is wrong, and the result is correct (then the following common errors are summarized)
    3. can use the structure , do not use the array, error-prone (temporarily do not know why)
    4. Code to specification, the space should have
    5. Some non-canonical expressions, different compilers have different results, note that you should avoid using such statements

Like this problem main pit on the 3rd, after the attention to avoid

The following is the AC code

#include <cstdio>#include<stack>#include<cstring>#include<cctype>Const intmaxn= ++Ten;using namespacestd;CharEXPS[MAXN];structMatrix {intA, B; Matrix (intA =0,intb =0): A (a), B (b) {}}m[ -];stack<Matrix>s;intMain () {#ifdef DEBUG freopen ("6.3.in","R", stdin); #endif    intN; scanf ("%d\r",&N);  for(intI=0; i<n;i++){        Chars0[Ten]; CharC; scanf ("%c",&c); s0[0]=C; scanf ("%d%d\r\n", &m[s0[0] -'A'].A, &m[s0[0] -'A'].b); //printf ("%c%d%d\r\n", s0[0], m[s0[0]-' A '].a, m[s0[0]-' a '].b);    }     while(SCANF ("%s", exps) = =1){        intsum=0; intlen=strlen (EXPS); intok=1;  for(intI=0; i<len;i++){            if(Isalpha (Exps[i])) {S.push (M[exps[i]-'A']); //printf ("Push%d%d \ n", m[exps[i]-' a '].a,m[exps[i]-' a '].b);            }            Else if(exps[i]==')') {Matrix m2=s.top (); S.pop (); //printf ("Pop%d%d \ n", m2.a, m2.b);Matrix M1 =s.top (); S.pop (); //printf ("Pop%d%d \ n", m1.a, m1.b);                if(m1.b! = m2.a) {ok=0; Break;} Sum+ = m1.a * m1.b *m2.b;             S.push (Matrix (m1.a, m2.b)); //printf ("Push%d%d \ n", m1.a, m2.b,);            }        }        if(OK) printf ("%d\n", sum); Elseprintf"error\n"); }    return 0;}

Example 6-3 Matrix Chain multiplication, Uva 442

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.