The main idea: to give a matrix multiplication formula, wherein the ranks of each matrix is known, to seek the final result.
The operation of the expression, about the use of the stack, the process of encountering two mismatched matrices, the matrix sequence error.
#include <stdio.h> #include <stdlib.h> #include <string.h>int a[60][3];char b[300];int c[300][2];int Top=0;int Main (void) {int i,n,arow,acol,brow,bcol,ok,count,j,le;scanf ("%d", &n); GetChar (); for (i=0;i<n;i++) { A[i][0]=getchar (); scanf ("%d%d", &a[i][1],&a[i][2]); GetChar ();} while (scanf ("%s", b) ==1) {Top=0;count=0;le=strlen (b); if (le==1) {b[0]= ' + ';p rintf ("0\n");} Else{ok=1;for (i=0;i<le;i++) {if (b[i]== ' (') {;} else if (b[i]== ') ') {brow=c[top][0];bcol=c[top][1];top--;arow=c[top][0];acol=c[top][1];top--;if (acol!=brow) {OK=0; break;} else{count=count+ (brow*arow*bcol); top++;c[top][0]=arow;c[top][1]=bcol;}} Else{for (j=0;j<n;j++) {if (b[i]==a[j][0]) {top++;c[top][0]=a[j][1];c[top][1]=a[j][2];break;}}}} for (j=0;j<le;j++) {b[j]=0;} if (ok==0) {printf ("error\n");} else{printf ("%d\n", Count);}}} return 0;}
UVA 442-matrix Chain multiplication (use of stacks)