inlinevoidmerge_product(int p)//处理当前的值和前一个值取'*'的操作 { int w0=f[p-1].v0*f[p].v0+f[p-1].v0*f[p].v1+f[p-1].v1*f[p].v0; int w1=f[p-1].v1*f[p].v1; f[p-1].v0=w0%Mod; f[p-1].v1=w1%Mod; }
intmain() { int n; cin>>n; f[0].v0=f[0].v1=1; while (n--) { now++; //新建一个空位读入新符号 cin>>f[now].ch; f[now].v0=f[now].v1=1; //初始化当前符号的前面的值(虽然')'除外,但也不影响) if (f[now].ch=='+') { if (f[now-1].ch=='*') //处理'*' { now--; merge_product(now); f[now]=f[now+1]; } if (f[now-1].ch=='+') //处理'+' { now--; merge_sum(now); f[now]=f[now+1]; } } if (f[now].ch=='*') if (f[now-1].ch=='*') //处理'*' { now--; merge_product(now); f[now]=f[now+1]; } if (f[now].ch==')') //处理')'(比较麻烦) { now--; if (f[now].ch=='*') { merge_product(now); now--; } if (f[now].ch=='+') { merge_sum(now); now--; } now--; f[now].v0=f[now+1].v0; f[now].v1=f[now+1].v1; if (f[now].ch=='*') { merge_product(now); now--; } } } if (f[now].ch=='*') //处理完了以后,可能还有残留的'*'和'+' { merge_product(now); now--; } if (f[now].ch=='+') { merge_sum(now); now--; } cout<<f[0].v0; return0; }
OOPS!
A comment box should be right here...But it was gone due to network issues :-(If you want to leave comments, make sure you have access to disqus.com.