#include<stdio.h>
#include<iostream>
#include<cstring>
#include<stack>
#include<string>
#define maxn 1000
using namespace std;
struct node
{
node() {}
node(char A,int x,int y) { this->a=A;this->x=x;this->y=y;lchild = NULL;rchild = NULL; }
char a;
int x,y;
node *lchild;
node *rchild;
};
struct Node
{
char ch;
int x,y;
};
void insert(node *&T, char c,int x,int y)//插入操作
{
if(!T) {T=new node(c,x,y);return;}
(T->a<c)?insert(T->rchild, c,x,y):insert(T->lchild, c,x,y);
}
void find(node *T,char c,int *x1,int *y1)
{
if(T->a==c)
{
*x1=T->x;
*y1=T->y;
return;
}
if(T->a<c)
{
find(T->rchild,c,x1,y1);
}
else find(T->lchild,c,x1,y1);
}
void Readchar(node *T)
{
char a[maxn];
char b[maxn];
Node p[100];
int i=0,len,d,j;
int x,y;
bool flag=false;
int sum=0;
stack <Node> s;
Node n;
while(scanf("%s",a)!=EOF)
{
len=strlen(a);
if(len==1)
{
printf("0\n");
continue;
}
sum=0;flag=false;
for(i=0;i<len&&!flag;i++)
{
if(a[i]=='(')//入栈
{
n.ch='(';
s.push(n);
}
else if(a[i]==')')//弹出栈
{
n.ch=')';
d=0;
while(s.top().ch!='('&&!s.empty())//栈空或者到左括号出循环
{
p[d++]=s.top();//保存在数组栈中
s.pop();
}//while
s.pop();//弹出左括号
x=p[d-1].x;y=p[d-1].y;
for(j=d-2;j>=0;j--)//计算改区间的值
{
if(p[j].x==y)
{sum+=(x*p[j].y*y);}
else //
{
printf("error\n");
flag=true;
break;
}
y=p[j].y;
}//for
n.ch='#';n.x=x;n.y=y;
s.push(n);//结果压入栈中
#include<iostream>
#include<cstring>
#include<stack>
#include<string>
#define maxn 1000
using namespace std;
struct node
{
node() {}
node(char A,int x,int y) { this->a=A;this->x=x;this->y=y;lchild = NULL;rchild = NULL; }
char a;
int x,y;
node *lchild;
node *rchild;
};
struct Node
{
char ch;
int x,y;
};
void insert(node *&T, char c,int x,int y)//插入操作
{
if(!T) {T=new node(c,x,y);return;}
(T->a<c)?insert(T->rchild, c,x,y):insert(T->lchild, c,x,y);
}
void find(node *T,char c,int *x1,int *y1)
{
if(T->a==c)
{
*x1=T->x;
*y1=T->y;
return;
}
if(T->a<c)
{
find(T->rchild,c,x1,y1);
}
else find(T->lchild,c,x1,y1);
}
void Readchar(node *T)
{
char a[maxn];
char b[maxn];
Node p[100];
int i=0,len,d,j;
int x,y;
bool flag=false;
int sum=0;
stack <Node> s;
Node n;
while(scanf("%s",a)!=EOF)
{
len=strlen(a);
if(len==1)
{
printf("0\n");
continue;
}
sum=0;flag=false;
for(i=0;i<len&&!flag;i++)
{
if(a[i]=='(')//入栈
{
n.ch='(';
s.push(n);
}
else if(a[i]==')')//弹出栈
{
n.ch=')';
d=0;
while(s.top().ch!='('&&!s.empty())//栈空或者到左括号出循环
{
p[d++]=s.top();//保存在数组栈中
s.pop();
}//while
s.pop();//弹出左括号
x=p[d-1].x;y=p[d-1].y;
for(j=d-2;j>=0;j--)//计算改区间的值
{
if(p[j].x==y)
{sum+=(x*p[j].y*y);}
else //
{
printf("error\n");
flag=true;
break;
}
y=p[j].y;
}//for
n.ch='#';n.x=x;n.y=y;
s.push(n);//结果压入栈中
}//if
else
{
n.ch=a[i];
find(T,a[i],&x,&y);
n.x=x;
n.y=y;
s.push(n);
}
else
{
n.ch=a[i];
find(T,a[i],&x,&y);
n.x=x;
n.y=y;
s.push(n);
}
}//for
if(!flag){printf("%d\n",sum);}
}
if(!flag){printf("%d\n",sum);}
}
}
int main()
{
int main()
{
int n,t;
char ch;
int x,y;
scanf("%d",&n);
t=n;
getchar();
scanf("%c",&ch);
scanf("%d%d",&x,&y);
node *T=new node(ch,x,y);
getchar();
while(--n)
{
scanf("%c",&ch);
scanf("%d%d",&x,&y);
insert(T,ch,x,y);
getchar();
}
Readchar(T);
char ch;
int x,y;
scanf("%d",&n);
t=n;
getchar();
scanf("%c",&ch);
scanf("%d%d",&x,&y);
node *T=new node(ch,x,y);
getchar();
while(--n)
{
scanf("%c",&ch);
scanf("%d%d",&x,&y);
insert(T,ch,x,y);
getchar();
}
Readchar(T);
return 0;
}