UVA 442

来源:互联网 发布:哪里有卖网络机顶盒的 编辑:程序博客网 时间:2024/05/17 02:25
/*这题思路简单,写起来有点小麻烦,建议用函数写,如有疑问,欢迎留言*/#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;}elsereturn 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;}elsestr[s] = 0;}if( time == 0 )return 1;if ( mul( a[0], a[1], s )  )str[s] = 'Z';elsereturn 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 );elseprintf( "error\n" );}return 0;}

原创粉丝点击