Matrix Chain Multiplication UVA 442 (栈 表达式求值)

说说:

其实这道题是栈这个数据结构最经典的运用,即表达式的求值。与一般情况不同的是,此次要求的运算数是矩阵。在整个解析表达式的过程中,无非遇到三类字符,一个是‘(‘,另一个是‘)’,剩下的就是运算数了。首先,在遇到‘(’的时候,栈指针自动加一,并将栈顶元素的行数和列数都设置为-1,这样就不会和正常的运算数混淆了。如果遇到的是运算数,首先要判断当前的栈顶元素是否为运算数(当然,还要注意栈为空的特殊情况)。若是,则直接将新的运算数与栈顶运算数进行计算,否则将新运算数入栈。还有最后一种情况就是遇到‘)’。此时,栈顶必为运算数,且栈顶的下一个元素必定是‘(’所以只要将栈顶元素下移一位即可。在这里有一点要特别注意,此时可能会出现两个栈的顶部出现连续的两个运算数(至多两个,三个是不可能的),因为在这种情况下,我们还要对这两个元素进行运算。不断重复上述步骤,整个题目就搞定啦!

源代码:

#include <stdio.h>
#define MAXN 26+5

typedef struct{
 int x;
 int y;
}Matrix;

int main(){
 int n,i,p,wrong,ans;
 char c;
 Matrix stack[MAXN],current,data[MAXN];
// freopen("data","r",stdin);
 scanf("%d\n",&n);
 for(i=0;i<n;i++)
   scanf("%c %d%d\n",&c,&data[i].x,&data[i].y);

 while(1){
  p=wrong=ans=0;
  while((c=getchar())!='\n'){
   if(c=='('){
     stack[p].x=stack[p].y=-1;//填充无效数,栈指针上移
     p++;
   }
   else if(c==')'){
     p--;
     stack[p-1].x=stack[p].x;//栈顶元素向下移动一位
     stack[p-1].y=stack[p].y;
     if(p-2>=0&&stack[p-2].x!=-1){//若此时栈顶存在连续两个运算数
      p--;
      if(stack[p-1].y!=stack[p].x)
        wrong=1;
      ans+=stack[p-1].x*stack[p-1].y*stack[p].y;
      stack[p-1].y=stack[p].y;
     }
   }
   else{
     if(p==0||stack[p-1].x==-1){
       stack[p].x=data[c-'A'].x;
       stack[p].y=data[c-'A'].y;
       p++;
     }
     else{
       if(stack[p-1].y!=data[c-'A'].x)
         wrong=1;
       ans+=stack[p-1].x*stack[p-1].y*data[c-'A'].y;
       stack[p-1].y=data[c-'A'].y;
     }
   }
  }

  if(wrong)//wrong为1标识着在运算过程中存在着不符合矩阵运算法则的情况
    printf("error\n");
  else
    printf("%d\n",ans);

  if((c=getchar())==EOF)
    break;
  else
    ungetc(c,stdin);
 }

 return 0;
}
时间: 2024-12-22 03:54:04

Matrix Chain Multiplication UVA 442 (栈 表达式求值)的相关文章

[2016-02-05][UVA][442][Matrix Chain Multiplication]

[2016-02-05][UVA][442][Matrix Chain Multiplication] UVA - 442 Matrix Chain Multiplication Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Suppose you have to evaluate an expression like A*B*C*D*E where

Matrix Chain Multiplication(表达式求值用栈操作)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1382    Accepted Submission(s): 905 Problem Description Matrix mul

UVa 442 Matrix Chain Multiplication(矩阵链乘,模拟栈)

题意  计算给定矩阵链乘表达式需要计算的次数  当前一个矩阵的列数等于后一个矩阵的行数时  他们才可以相乘  不合法输出error 输入是严格合法的  即使只有两个相乘也会用括号括起来  而且括号里最多有两个 那么就很简单了 遇到字母直接入栈  遇到反括号计算后入栈  然后就得到结果了 #include<cstdio> #include<cctype> #include<cstring> using namespace std; const int N = 1000;

stack UVA 442 Matrix Chain Multiplication

题目传送门 /* stack 容器的应用:矩阵的表达式求值 A 矩阵是a * b,B 矩阵是b * c,则A * B 是a * c */ #include <cstdio> #include <iostream> #include <algorithm> #include <stack> #include <cmath> #include <cstring> #include <string> using namespac

UVA 442 二十 Matrix Chain Multiplication

Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 442 Appoint description:  System Crawler  (2015-08-25) Description Suppose you have to evaluate an expression like A*B*C*D*E

442 - Matrix Chain Multiplication

Matrix Chain Multiplication Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the number of

UVa442 Matrix Chain Multiplication (栈)

链接:http://acm.hust.edu.cn/vjudge/problem/19085分析:用栈来实现一个简单的表达式求值.输入表达式保证合法,比如(A(BC))把BC看成一个整体D那么(A(BC))就是AD,所以这些表达式都是在不断重复嵌套相同的结构A*B.遍历表达式,遇到字母则将对应矩阵压入栈中,遇到右括号则将栈顶两个矩阵弹出,判断是否能进行乘法,如果能则累加乘法次数,再将相乘后的矩阵压栈,如果不能则将error置为true,退出循环. 1 #include <iostream> 2

UVA Matrix Chain Multiplication

题目如下: Matrix Chain Multiplication Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the numb

ACM学习历程——UVA442 Matrix Chain Multiplication(栈)

Description Matrix Chain Multiplication  Matrix Chain Multiplication  Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are perfo