Portal: http://oj.cnuschool.org.cn/oj/home/solution.htm?solutionID=35454
20603 matrix Chain Multiplication |
Difficulty level: B; run time limit: 1000ms; operating space limit: 51200KB; code length limit: 2000000B |
Question Description |
Enter the dimensions of n matrices and the expressions of some matrix chains, and the number of times the multiplication is output. If multiplication is not possible, output error. Assuming that a is a m*n matrix and B is a n*p matrix, the number of times to multiply is m*n*p. If the number of columns in matrix A is not equal to the number of rows in matrix B, then the two matrices cannot be multiplied. For example: A is 50*10, B is 10*20, C is 20*5, then a (BC) is multiplied by 10*20*5 (BC multiplication) +50*10*5 (A (BC) multiplication) =3500. The data in question was incorrect and was revised in 2015-4-25. |
Input |
The first line consists of a positive integer n, representing a total of n matrix participation operations. The next n rows, each line consists of three parts, the first part is the name of the matrix (one capital letter), the second and the third part are a positive integer, each representing the number of rows and columns of the Matrix, the three parts are separated by a space. The last line includes a valid string for a matrix operation (including parentheses and the name of the matrix above) |
Output |
Output as required in the title description
|
Input example |
3 A 50 10 B 10 20 C 20 5 A (BC) |
Output example |
3500 |
Other Notes |
Data range: The length of the expression does not exceed 100 characters. All data operations will not exceed the Int32 range. |
Stack handles simple expressions.
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 #include < Cstring> 6 #include <stack> 7 using namespace std; 8 const int MAXN=100+10; 9 int n;10 struct MATRIX{11 int a,b;12}m[maxn];13 inline int read () {int. int X=0,sig=1;char Ch=getchar (); whi Le (!isdigit (CH)) {if (ch== '-') Sig=-1;ch=getchar ();} + while (isdigit (CH)) x=10*x+ch-' 0 ', Ch=getchar (),}19 return x*=sig;18, inline void write (int x) {if (x==0) { Putchar (' 0 '); return;} if (x<0) Putchar ('-'), x=-x;21 int len=0,buf[15]; while (x) buf[len++]=x%10,x/=10;22 for (int i=len-1;i>=0;i--) Putchar (buf[i]+ ' 0 '); return;23}24 int Solve (matrix& ; A,matrix b) {ans=-1;26 int. if (A.B==B.A) {ans=a.a*b.b*b.a;a.b=b.b;} ans;28}29 void init () {n=read (); Char ch;32 for (int i=1;i<=n;i++) {$ do Ch=getch AR (); while (Isalpha (CH)!=1); int id=ch-' A '; M[id].a=read ();//write (M[ID].A); Putchar (' \ n '); PNs M[id].b=read ();//write (m[id].b); Putchar (' \ n ');}40 return;41}42 stack<matrix> s;43 void work () {A. int ans=0;45 char S[MAXN];SCANF ("%s", s); Len=strlen int (s); i=0;i<len;i++ (int) {isalpha (s[i]) s.push (m[s[i]-' A ']); 49 else if (s[i]== ') ') {Matrix t2=s.top (); S.pop (); Wuyi Matrix t1=s.top (); S.pop (); t=solve int (T1,T2); if (t<0) {puts ("error"); return;} ans+=t; S.push (T1),}56}57 Matrix t2=s.top (); S.pop (); t1=s.top Matrix (); S.pop (); t=solve int (T1,T2); if (t<0) {puts ("error"); return;} Ans+=t;62 write (ans), return;64}65 void print () {return;67}68 int main () {;p RI NT (); return 0;70
}
POJ 0016 20603 matrix chain multiplication