解题思路:
(1)两个栈
(2)注意栈为空的情况
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
int helper(vector<vector<int>> &v,string &s) {
stack<char> s1;
stack<pair<int,int>> s2;
int count=0,sum=0;
for(int i=0;i<s.length();i++) {
if(s[i]=='(') s1.push(s[i]);
else if('A'<=s[i] && s[i]<='Z') {
s2.push({v[count][0],v[count][1]});
count++;
} else {
if(s2.empty()) return sum;
auto a=s2.top();
s2.pop();
if(s2.empty()) return sum;
auto b=s2.top();
s2.pop();
sum+=b.first*b.second*a.second;
s2.push({b.first,a.second});
s1.pop();
}
}
return sum;
}
int main() {
int n;
string s;
while(cin>>n) {
vector<vector<int>> v(n,vector<int>(2,0));
for(int i=0;i<n;i++) {
cin>>v[i][0]>>v[i][1];
}
cin>>s;
cout<<helper(v,s)<<endl;
}
return 0;
}