这道题思路很简单,遇到字母就入栈,遇到‘)’出栈。
#include <iostream>
#include <stdio.h>
#include <stack>
using namespace std;
struct node
{
int le, ri;
}alph[26];
bool judge(char c)
{
if(c>='A'&&c<='Z')
return true;
return false;
}
int main()
{
for(int i=0; i<26; i++)
alph[i].le = alph[i].ri = 0;
int n, left, right;
scanf("%d", &n);
char alp;
while(n--)
{
cin >> alp;
scanf("%d", &alph[alp-'A'].le);
scanf("%d", &alph[alp-'A'].ri);
}
// for(int i=0; i<26; i++)
// cout << alph[i].le << " " << alph[i].ri << endl;
string str;
stack<node> st;
while(cin >> str)
{
int ans = 0, flag = 0;
int len = str.length();
for(int i=0; i<len; i++)
{
if(judge(str[i]))
st.push(alph[str[i]-'A']);
else if(str[i]==')')
{
node a = st.top();
st.pop();//后
node b = st.top();
st.pop();//前
if(b.ri!=a.le)
{
flag = 1; break;
}
else
{ans += b.le*b.ri*a.ri;node c; c.le = b.le; c.ri = a.ri;st.push(c);}
}
}
if(!flag)
cout << ans << endl;
else
cout << "error\n";
}
return 0;
}