uva442 Matrix Chain Multiplication

简介: uva442 Matrix Chain Multiplication
#include <stdio.h>#define LOCAL#define MAXN 200typedefstructelem{
charmatrix;
introw;
intcol;
}elem;
typedefstructstackelem{
introw;
intcol;
}elemstack;
elemdata[26];
elemstackstack[MAXN];
intmain()
{
intn;
inti;
charstr[2];
charc;
inttop=-1;
elemstacktemp, a, b;
intmulcount;
interror;
#ifdef LOCALfreopen("c://uva_in.txt", "r", stdin);
#endifscanf("%d", &n);
for (i=0; i<n; i++)
    {
scanf("%s%d%d", str, &(data[i].row), &(data[i].col));
data[i].matrix=str[0];
    }
fgetc(stdin);
mulcount=0;
error=0;
while((c=fgetc(stdin)) !=EOF)
    {
if (c=='/n')
        {
if (error)
printf("error/n");
elseprintf("%d/n", mulcount);
top=-1;
mulcount=0;
error=0;
        }
elseif (c!='('&&c!=')')
        {
for (i=0; i<n; i++)
            {
if (data[i].matrix==c)
                {
temp.row=data[i].row;
temp.col=data[i].col;
break;
                }
            }
stack[++top] =temp;
        } elseif (c==')')
        {
b=stack[top--];
a=stack[top--];
if (a.col==b.row)
            {
temp.row=a.row;
temp.col=b.col;
stack[++top] =temp;
mulcount+=a.row*a.col*b.col;
            } elseerror=1;
        }
        }
return0;
}
目录
相关文章
|
存储 索引
LeetCode 73. Set Matrix Zeroes
给定一个m * n 的矩阵,如果当前元是0,则把此元素所在的行,列全部置为0. 额外要求:是否可以做到空间复杂度O(1)?
101 0
LeetCode 73. Set Matrix Zeroes
UVA442 矩阵链乘 Matrix Chain Multiplication
UVA442 矩阵链乘 Matrix Chain Multiplication
|
人工智能 BI
CodeForces - 1485D Multiples and Power Differences (构造+lcm)
CodeForces - 1485D Multiples and Power Differences (构造+lcm)
82 0
C-POJ-1426 Find The Multiple
Description Given a positive integer n, write a program to find out a nonzero multiple m o...
964 0