P1310 表达式的值 (表达式计算)
Posted feiief
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P1310 表达式的值 (表达式计算)相关的知识,希望对你有一定的参考价值。
解法:
栈
1 #include <bits/stdc++.h> 2 # define LL long long 3 using namespace std; 4 5 const int mod=10007; 6 stack<char> ope; 7 stack<int> one; 8 stack<int> zero; 9 10 void calc(){ 11 char o=ope.top(); 12 ope.pop(); 13 int one1=one.top(); 14 one.pop(); 15 int one2=one.top(); 16 one.pop(); 17 int zero1=zero.top(); 18 zero.pop(); 19 int zero2=zero.top(); 20 zero.pop(); 21 int t1; 22 int t0; 23 if(o==‘*‘){ 24 t1=(one1*one2)%mod; 25 t0=((one1*zero2)%mod+(one2*zero1)%mod+(zero1*zero2)%mod)%mod; 26 }else { 27 t1=((zero1*one2)%mod+(one1*zero2)%mod+(one2*one1)%mod)%mod; 28 t0=(zero1*zero2)%mod; 29 } 30 one.push(t1); 31 zero.push(t0); 32 } 33 34 int main(){ 35 int L; 36 cin>>L; 37 string s; 38 cin>>s; 39 s=‘(‘+s+‘)‘; 40 41 for(int i=0;i<s.size();++i){ 42 if(s[i]==‘(‘){ 43 ope.push(s[i]); 44 if(s[i+1]!=‘(‘){ 45 one.push(1); 46 zero.push(1); 47 } 48 }else if(s[i]==‘+‘){ 49 while(ope.top()!=‘(‘){ 50 calc(); 51 } 52 ope.push(s[i]); 53 if(s[i+1]!=‘(‘){ 54 one.push(1); 55 zero.push(1); 56 } 57 }else if(s[i]==‘*‘){ 58 ope.push(s[i]); 59 if(s[i+1]!=‘(‘){ 60 one.push(1); 61 zero.push(1); 62 } 63 }else if(s[i]==‘)‘){ 64 while(ope.top()!=‘(‘){ 65 calc(); 66 } 67 ope.pop(); 68 } 69 } 70 printf("%d", zero.top()); 71 return 0; 72 }
2. 先转换成后缀表达式,再计算
1 #include <bits/stdc++.h> 2 # define LL long long 3 using namespace std; 4 5 const int mod=10007; 6 stack<char> ope; 7 stack<int> one; 8 stack<int> zero; 9 10 11 int main(){ 12 string s; 13 int L; 14 cin>>L; 15 cin>>s; 16 string str; 17 //补全需要填数的位置 18 for(int i=0;i<L;++i){ 19 if(s[i]==‘(‘) { 20 str+=‘(‘; 21 }else { 22 if(i==0 || (i>0 && s[i-1]!=‘)‘)){ 23 str+=‘n‘; 24 } 25 str+=s[i]; 26 } 27 } 28 if(str[str.size()-1]==‘+‘ || str[str.size()-1]==‘*‘){ 29 str+=‘n‘; 30 } 31 32 //中缀转后缀 33 vector<char> suff; 34 for(int i=0;i<str.size();++i){ 35 if(str[i]==‘n‘){ 36 suff.push_back(str[i]); 37 }else if(str[i]==‘(‘ || str[i]==‘*‘){ 38 ope.push(str[i]); 39 }else if(str[i]==‘+‘){ 40 while(!ope.empty() && ope.top()==‘*‘){ 41 ope.pop(); 42 suff.push_back(‘*‘); 43 } 44 ope.push(str[i]); 45 }else{ 46 while(!ope.empty() && ope.top()!=‘(‘){ 47 suff.push_back(ope.top()); 48 ope.pop(); 49 } 50 ope.pop(); 51 } 52 } 53 while(!ope.empty()){ 54 suff.push_back(ope.top()); 55 ope.pop(); 56 } 57 58 for(int i=0;i<suff.size();++i){ 59 if(suff[i]==‘n‘){ 60 one.push(1); 61 zero.push(1); 62 }else if(suff[i]==‘+‘){ 63 int o1=one.top(); one.pop(); 64 int o2=one.top(); one.pop(); 65 int z1=zero.top(); zero.pop(); 66 int z2=zero.top(); zero.pop(); 67 int t1=((o1*z2)%mod+(z1*o2)%mod+(o1*o2)%mod)%mod; 68 int t0=(z1*z2)%mod; 69 one.push(t1); 70 zero.push(t0); 71 }else if(suff[i]==‘*‘){ 72 int o1=one.top(); one.pop(); 73 int o2=one.top(); one.pop(); 74 int z1=zero.top(); zero.pop(); 75 int z2=zero.top(); zero.pop(); 76 int t1=(o1*o2)%mod; 77 int t0=((o1*z2)%mod+(z1*o2)%mod+(z1*z2)%mod)%mod; 78 one.push(t1); 79 zero.push(t0); 80 } 81 } 82 printf("%d", zero.top()); 83 return 0; 84 }
以上是关于P1310 表达式的值 (表达式计算)的主要内容,如果未能解决你的问题,请参考以下文章