/*这题思路简单,写起来有点小麻烦,建议用函数写,如有疑问,欢迎留言*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int p[200][200], top, flag, sum;
char stack[200];
char str[2000];
typedef struct matrix
{
char n;
int row, col;
}M;
int mul( int b, int e, int s )
{
if( p[b][1] == p[e][0] )
{
p[s][0] = p[b][0];
p[s][1] = p[e][1];
sum += p[b][0] * p[b][1] * p[e][1];
return 1;
}
else
return 0;
}
int deal( int len )
{
int a[10];
int s, k, i, time;
for( i = 0; i < len; i++ )
{
if( str[i] == '(' )
{
stack[top++] = str[i];
continue;
}
if( str[i] == ')' && stack[top-1] == '(' )
{
time = 0;
k = i;
for( k = i; str[k] != '(' ; k-- );
for( s = k; str[s] != ')' ; s++ )
{
if( isalpha(str[s]) )
{
a[time++] = s;
str[s] = 0;
}
else
str[s] = 0;
}
if( time == 0 )
return 1;
if ( mul( a[0], a[1], s ) )
str[s] = 'Z';
else
return 0;
}
}
}
int main()
{
int N, len, i, j;
char temp1[20], temp2[20], temp3[20];
M mat[200];
scanf( "%d", &N );
for( i = 0; i < N; i++ )
{
scanf( "%s%s%s", temp1, temp2, temp3 );
mat[i].n = temp1[0];
mat[i].row = atoi(temp2);
mat[i].col = atoi(temp3);
}
while( scanf( "%s", str ) != EOF )
{
sum = 0;
memset(p, 0, sizeof(p) );
len = strlen(str);
if( len == 1 )
{ printf( "0\n" ); continue; }
for( i = 0; i < len; i++ )
{
if( isalpha(str[i]) )
{
for( j = 0; j < N; j++ )
if( mat[j].n == str[i] )
{
p[i][0] = mat[j].row;
p[i][1] = mat[j].col;
break;
}
}
}
if ( deal( len ) )
printf( "%d\n", sum );
else
printf( "error\n" );
}
return 0;
}
UVA 442
最新推荐文章于 2021-01-27 17:18:44 发布