UVA 442

/*这题思路简单,写起来有点小麻烦,建议用函数写,如有疑问,欢迎留言*/
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值