我写的是表达式栈啊
#include<bits/stdc++.h>
using namespace std;
namespace estidi{
int mian(){
int n,t;
string s,rs="";
stack<int>op;
stack<pair<int,int>>num;
pair<int,int>x,y;
scanf("%d",&n);
getchar();
cin>>s;
s=" "+s+" ";
for(int i=0;i<=n;i++){
rs+=s[i];
if(s[i]!=')'&&s[i+1]!='(')
rs+="0";
}
s=rs+')';
n=s.size()-1;
op.push(3);
for(int i=1;i<=n;i++){
switch(s[i]){
case '0':
num.push(make_pair(1,1));
break;
case '(':
op.push(3);
break;
case ')':
while(op.top()!=3){
t=op.top();
op.pop();
x=num.top();
num.pop();
y=num.top();
num.pop();
if(t==2)
num.push(make_pair((x.first*y.first)%10007,((x.first+x.second)*(y.first+y.second)-x.first*y.first)%10007));
else
num.push(make_pair(((x.first+x.second)*(y.first+y.second)-x.second*y.second)%10007,x.second*y.second%10007));
}
op.pop();
break;
case '+':
while(op.size()&&op.top()==1){
op.pop();
x=num.top();
num.pop();
y=num.top();
num.pop();
num.push(make_pair(((x.first+x.second)*(y.first+y.second)-x.second*y.second)%10007,x.second*y.second%10007));
}
op.push(2);
break;
case '*':
op.push(1);
break;
default:
throw s[i];
}
stack<int>opp=op;
stack<pair<int,int>>numm=num;
// cerr<<op.size()<<" "<<num.size()<<"\n\n";
// while(opp.size()){
// cerr<<"|";
// if(opp.top()==3)
// cerr<<"(";
// else
// if(opp.top()==2)
// cerr<<"+";
// else
// cerr<<"*";
// cerr<<"|\n";
// opp.pop();
// }
// cerr<<"---\n\n";
// while(numm.size()){
// cerr<<"|"<<numm.top().first<<" "<<numm.top().second<<"|\n";
// numm.pop();
// }
// cerr<<"---\n\n";
}
printf("%d",num.top().first);
return 0;
}
}
int main(){
estidi::mian();
return 0;
}
怎么就挂了???