UVA 442-Matrix Chain Multiplication(栈的运用)

题目大意:给出一个矩阵乘法式子,其中每个矩阵的行列已知,求最终的结果。

表达式的运算,关于栈的运用,运算过程中碰到两个不匹配的矩阵时,则矩阵序列错误。

#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]='\0';
			printf("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;
}
时间: 2024-10-06 18:17:52

UVA 442-Matrix Chain Multiplication(栈的运用)的相关文章

[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

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(矩阵链乘,模拟栈)

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

UVa 442 Matrix Chain Multiplication(栈的应用)

题目链接: https://cn.vjudge.net/problem/UVA-442 1 /* 2 问题 3 输入有括号表示优先级的矩阵链乘式子,计算该式进行的乘法次数之和 4 5 解题思路 6 栈的应用,直接忽视左括号,每次只计算栈顶的两个矩阵会更加方便. 7 */ 8 #include<cstdio> 9 #include<cstring> 10 #include<stack> 11 #include<cctype> 12 using namespac

UVA - 442 Matrix Chain Multiplication(栈模拟水题+专治自闭)

题目: 给出一串表示矩阵相乘的字符串,问这字符串中的矩阵相乘中所有元素相乘的次数. 思路: 遍历字符串遇到字母将其表示的矩阵压入栈中,遇到‘)’就将栈中的两个矩阵弹出来,然后计算这两个矩阵的元素相乘的次数,累加就可以了. PS:注意弹出的矩阵表示的先后顺序. 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1000000000 #define mod 1000000007 #define FRE() freopen

UVA - 442 Matrix Chain Multiplication

点击打开链接 题目意思是求矩阵相乘的运算次数, 设A size为n*s,B size为s*m 那么A*B运算量为n*m*s. 注意括号里面的优先级,并且依次累加即可,并且没有不合法的序列. 思路是先对输入的n个矩阵编号按照字典序排序,因为每次两个矩阵相乘会得到一个新的矩阵,编号可以设置成在n的编号加1,并且重新压入栈中. #include <iostream> #include <cstdio> #include <cmath> #include <vector&

UVA - 442 Matrix Chain Multiplication 双端队列

题目大意:给出n个矩阵和表达式,问该表达式是否正确,如果计算正确,输出计算了多少次 解题思路:双端队列,遇到右括号时弹出后面的两个矩阵进行乘法,相乘时要注意顺序,是第二个出队列的乘上第一个出队列的. #include<cstdio> #include<algorithm> #include<deque> #include<map> #include<cstring> using namespace std; #define maxn 50010

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

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

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