Meaning of the question: Given n strings, then at the beginning is +, if you encounter a parenthesis, then + becomes *. Or * becomes +; what is the value of the expression after the left; so this question can be written by simulation;
concrete understanding:
one of the more ingenious questions in this question is that this is initialized to +, as long as it meets then it will change So you can write it directly with stack; one puts the operator and one puts the number, but pay attention to this point: in
this case, remember to remove the extra plus sign;
I didn’t put the (into The stack is directly relied on to change the number to enter the stack, but I don’t know why it is wa. In the end, I put the (and it will pass, but this is after the game... It’s really unlucky.: D
AC code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll Mod=1e9+7;
char fun(char t){
if(t=='+')return '*';
else return '+';
}
int main(){
ll n;
scanf("%lld",&n);
string s;
char f='+',t;
stack<char > st;
stack<ll > num;
for(ll i=0;i<n;i++){
//其实理解上上面画的图,其实就可以自己模拟了
cin>>s;
if(s[0]=='('){
f=fun(f);
st.push('(');
}else if(s[0]==')'){
//注意如果遇见)那么需要先pop一个符号,不然会有多余的
if(st.size())st.pop();
while(st.top()!='('){
if(f=='+'){
ll t1=num.top();num.pop();
ll t2=num.top();num.pop();
num.push((t1+t2)%Mod);
}else if(f=='*'){
ll t1=num.top();num.pop();
ll t2=num.top();num.pop();
num.push(t1*t2%Mod);
}
st.pop();
}
st.pop();
f=fun(f);//注意,这里是需要计算完之后才能变号的
st.push(f);
//cout<<f<<endl;
}else{
ll t=0;
for(ll i=0;i<s.size();i++){
t=t*10+s[i]-'0';
}
num.push(t%Mod);
st.push(f);
//cout<<f<<endl;
}
}
if(st.size())st.pop();
while(num.size()>1){
//这里是把剩余的数计算出来
if(st.top()=='+'){
ll t1=num.top();num.pop();
ll t2=num.top();num.pop();
num.push((t1+t2)%Mod);
}else if(st.top()=='*'){
ll t1=num.top();num.pop();
ll t2=num.top();num.pop();
num.push((t1*t2)%Mod);
}
st.pop();
}
cout<<num.top()%Mod<<endl;
return 0;
}