442 - Matrix Chain Multiplication
Posted: Sun Jun 13, 2004 2:44 pm
wud anybody plz provide me some tricky inputs for the problem 442??
Code: Select all
#include<stdio.h>
#include<ctype.h>
#include<string.h>
struct m{
char ch;
long row;
long col;
};
char stack[1000];
int main()
{
struct m matrix[150],first,second,tmp;
char str[10000];
char ch_index = 'a'-1;
char a,b,t;
int test_case;
int i,j;
int index,flag,error_flag;
unsigned long res;
int matrix_index = -1;
//freopen("442.txt","r",stdin);
scanf("%d",&test_case);
scanf("%c",&t);
for(i = 0;i<test_case;i++)
{
scanf("%c",&t);
scanf("%ld %ld",&matrix[t].row,&matrix[t].col);
scanf("%c",&t);
}
while(gets(str)!= NULL)
{
i = 0;
res = 0;
index = -1;
flag = 0;
error_flag = 0;
while(str[i])
{
if( isupper(str[i]) ==1)
{
stack[++index] = str[i];
}
else if (str[i] == ')')
{
flag = 1;
b = stack[index];
index --;
a = stack[index];
index--;
first = matrix[a];
second = matrix[b];
if(first.col != second.row)
{
printf("error\n");
error_flag = 1;
break;
}
else
{
tmp.ch = ++ch_index;
tmp.row = first.row;
tmp.col = second.col;
matrix[tmp.ch] = tmp;
stack[++index] = tmp.ch;
res+= first.row * first.col* second.col;
}
}
i++;
}
if(flag ==0 && error_flag ==0)
printf("0\n");
else if(error_flag == 0)
printf("%lu\n",res);
}
}
Code: Select all
if( isupper(str[i]) ==1)
Code: Select all
if( isupper(str[i]))
These helps to get ACexpression();
if ( Error) return;
expression();
if ( Error) return;
Code: Select all
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
void push(char ch);
struct m{
long row;
long col;
};
char stack[200];
int top = 100;
int main()
{
struct m matrix[100];
long test,result,i,flag;
char ch,a,b,str[100];
cin >> test;
while(test--)
{
cin>>ch;
cin>> matrix[ch].row>> matrix[ch].col;
}
while(gets(str))
{
result = 0;
for(i = 0; str[i]; i++)
{
if(isupper(str[i]))
push(str[i]);
else if(str[i]==')')
{
b = stack[top-1];
a = stack[top-2];
flag = 1;
if(matrix[a].col != matrix[b].row)
{
flag = 0;
cout<<"error"<<endl;
break;
}
top-=2;
result += matrix[a].row *matrix[a].col*matrix[b].col;
ch = (char)top;
matrix[top].row = matrix[a].row;
matrix[top].col = matrix[b].col;
stack[top++] = ch;
}
}
if(flag)
cout<<result<<endl;
}
return 0;
}
void push(char ch)
{
stack[top++] = ch;
}
Innocent code!! Really? Try the samplesjainal cse du wrote:hello everebody
i am so bored with the chilly problem. i cant find the problem in my innocent code.
sample outputs are okey.
but why WA?
Code: Select all
3
A 50 10
B 10 20
C 20 5
A
B
C
()
Code: Select all
0
0
0
error
Code: Select all
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
struct matriz{
char c;
int filas,cols;
};
int main(void)
{
int n=0;
scanf("%d\n",&n);
matriz dict[30];
for(int i = 0; i < n;i++){
matriz m;
scanf("%c %d %d\n",&(m.c),&(m.filas),&(m.cols));
dict[m.c-'A']=m;
}
char exp[1000];
while(1){
if(scanf("%s",exp)!=1) break;
stack<matriz> s;
double total = 0;
matriz thenew;
int flag = 0;
for(int j = 0; j < strlen(exp);j++){
char car = exp[j];
if(car>='A' && car<='Z'){
s.push(dict[car-'A']);
}
else if(car==')'){
if(s.size()==0) {
flag = 1;
break;
}
matriz sale1 = s.top();
s.pop();
matriz sale2 = s.top();
s.pop();
if(sale1.cols==sale2.filas || sale2.cols==sale1.filas){
if(sale2.cols==sale1.filas){
matriz auxi = sale1;
sale1=sale2;
sale2=auxi;
}
thenew.filas = sale1.filas;
thenew.cols = sale2.cols;
s.push(thenew);
total+=(sale1.cols*sale1.filas*sale2.cols);
}
else{
flag = 1;
break;
}
}
}
if(flag)printf("error\n");
else printf("%g\n",total);
}
}
Code: Select all
9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
(CG(HI))
(AB(C))
(A)
Code: Select all
6250
error
error
Code: Select all
()
(CG(HI))
(AB(C))
Code: Select all
Expression = Matrix | "(" Expression Expression ")"
Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"
Missed that line.The second part of the input file strictly adheres to the following syntax (given in EBNF):
Code: Select all
Code deleted.