Advertisement
juanjo12x

UVA_442_Matrix_Chain_Multiplication

Aug 6th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <string>
  6. #include <cctype>
  7. #include <stack>
  8. #include <queue>
  9. #include <list>
  10. #include <vector>
  11. #include <map>
  12. #include <set>
  13. #include <sstream>
  14. #include <stdlib.h>
  15. #include <cmath>
  16. #define LL unsigned long long
  17. using namespace std;
  18. typedef struct m{
  19.     char car;
  20.     int r;
  21.     int c;
  22. }Tmat;
  23. Tmat arr[27];
  24.  
  25. int main() {
  26.  int ro,col,index,aux,n,result;
  27.  char c;Tmat au;
  28.  string cad;
  29.   stack<Tmat> s;
  30.   bool fallo;
  31.   Tmat s1,s2;
  32.   scanf("%d",&n);
  33.   int cont=0;getchar();
  34.   for (int i=0;i<n;i++){
  35.     scanf("%c %d %d",&c,&ro,&col);
  36.     arr[i].car=c;arr[i].r=ro;arr[i].c=col;
  37.         getchar();
  38.   }
  39.   fallo=false;
  40.   while(cin>>cad){
  41.     result=0;index=0;fallo=false;
  42.     n=cad.length();
  43.     if(n==1){printf("0\n");}
  44.     else{
  45.       while(index<=n-1 && !fallo){
  46.         if(isalpha(cad[index])){
  47.             cont=0;
  48.              /*busco el correspondiente car*/
  49.              while(arr[cont].car!=cad[index]){
  50.                 cont++;
  51.              }
  52.              au=arr[cont];
  53.             s.push(au);
  54.             index++;
  55.         }else if(cad[index]==')'){
  56.             s1=s.top();s.pop();s2=s.top();s.pop();
  57.             if(s1.r!=s2.c){fallo=true;continue;}
  58.             else{
  59.              result+=(s1.c*s1.r*s2.r);
  60.              aux=s1.c;
  61.              s1.r=s2.r; s1.c=aux;
  62.              s.push(s1);
  63.              index++;
  64.              }
  65.         }else{
  66.             index++;
  67.         }
  68.     }
  69.        if(!fallo){
  70.     printf("%d\n",result);
  71.        }else printf("error\n");
  72.     }
  73.    
  74.     getchar();
  75.   }
  76.   return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement