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)?
93 0
LeetCode 73. Set Matrix Zeroes
UVA442 矩阵链乘 Matrix Chain Multiplication
UVA442 矩阵链乘 Matrix Chain Multiplication
|
人工智能
Kuroni and Impossible Calculation——容斥原理-鸽笼原理-抽屉原理
题目描述 已知一个数组a[n],请计算式子:∏_{1≤i<j≤n}|ai−aj| 的值,其中1<=i,j<=n;我们可以认为,这一式子等价于 |a1−a2|⋅|a1−a3|⋅ … ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ … ⋅|a2−an|⋅ … ⋅|an−1−an|
114 0
Kuroni and Impossible Calculation——容斥原理-鸽笼原理-抽屉原理
|
人工智能
POJ 3673 Cow Multiplication
Cow Multiplication Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13312   Accepted: 9307 Description Bessie is t...
879 0