UVA Solution 442 - Matrix Chain Multiplication | Solution in C, C++ - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Friday, May 12, 2017

UVA Solution 442 - Matrix Chain Multiplication | Solution in C, C++

UVA Solution 442 - Matrix Chain Multiplication - Volume 4


UVA Online Judge Solution 442 - Matrix Chain Multiplication| Volume 4
UVA Problem Link - https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=383

Problem Name: 442 - Matrix Chain Multiplication
Problem Number : UVA - 442 - Matrix Chain Multiplication
Online Judge : UVA Online Judge Solution
Volume: 4
Solution Language : C, C plus plus

UVA Online Judge Solution, UVA OJ Solution list, UVA Problems Solution, UVA solver, UVA all problem solution list

UVA Solution 400 Code in C/CPP:

#include <stdio.h>
int n, i, j, k;
int alpha[105][2];
char s[105];
void solve() {
    int stack[105], sidx = -1;
    int sum = 0, alidx = 'Z';
    for(i = 0; s[i]; i++) {
        if(s[i] == '(') {
            stack[++sidx] = '(';
        } else if(s[i] == ')'){
            j = sidx;
            while(stack[j] != '(')
                j--;
            for(k = j+1; k < sidx; k++) {
                if(alpha[stack[k]-'A'][1] != alpha[stack[k+1]-'A'][0]) {
                    puts("error");
                    return;
                }
                sum += alpha[stack[k]-'A'][0]*alpha[stack[k]-'A'][1]*alpha[stack[k+1]-'A'][1];
            }
            alidx++;
            alpha[alidx-'A'][0] = alpha[stack[j+1]-'A'][0];
            alpha[alidx-'A'][1] = alpha[stack[sidx]-'A'][1];
            stack[j] = alidx;
            sidx = j;
        } else
            stack[++sidx] = s[i];
    }
    printf("%d\n", sum);
}
int main() {
    int x, y;
    scanf("%d", &n);
    for(i = 0; i < n; i++) {
        scanf("%s %d %d", s, &x, &y);
        alpha[s[0]-'A'][0] = x;
        alpha[s[0]-'A'][1] = y;
    }
    while(scanf("%s", s) == 1) {
        solve();
    }
    return 0;
}


Tags: UVA Online Judge Solution, UVA OJ Solution list, UVA Problems Solution, UVA solver, UVA all problem solution list, UVA Matrix Chain Multiplication  code in C, UVA 442  code in C++, UVA 442 solution in C, UVA Matrix Chain Multiplication solution

No comments:

Post a Comment