Problem Description
John has never been very good at maths. Due to his bad grades, his parents have sent him to the Academic Coalition of Mathematics (ACM). Despite the large amount of money his parents are spending on the ACM,
John does not pay much attention during classes. However, today he has begun to think about all the e ffort his parents are putting into his education, and he has started to feel somewhat. . . guilty. So he has made a decision: he is going to improve his maths grades!
However, no sooner had he resolved to pay attention than the lesson ended. So the only thing he has been able to do is to hurriedly copy the content of the blackboard in his notebook. Today, the teacher was explaining basic arithmetic expressions with unknowns. He vaguely remembers that his classmates have been substituting values into the unknowns to obtain the expressions' results. However, in all the hurry, John has only written down expressions, values and results in a messy fashion. So he does not know which value comes with each unknown, or which result goes with each expression.
That is the reason he needs your help: he wants to know, given an expression, some values and a result, whether it is possible or not to assign those values to the unknowns in order for the expression to evaluate to the given result. The particular assignment of values does not matter to John, as he wants to do it by himself. He only wants to know whether it is possible or not.
John does not pay much attention during classes. However, today he has begun to think about all the e ffort his parents are putting into his education, and he has started to feel somewhat. . . guilty. So he has made a decision: he is going to improve his maths grades!
![](https://i-blog.csdnimg.cn/blog_migrate/7d4a1ac3dacb73e4d2cd5157c8f48122.jpeg)
However, no sooner had he resolved to pay attention than the lesson ended. So the only thing he has been able to do is to hurriedly copy the content of the blackboard in his notebook. Today, the teacher was explaining basic arithmetic expressions with unknowns. He vaguely remembers that his classmates have been substituting values into the unknowns to obtain the expressions' results. However, in all the hurry, John has only written down expressions, values and results in a messy fashion. So he does not know which value comes with each unknown, or which result goes with each expression.
That is the reason he needs your help: he wants to know, given an expression, some values and a result, whether it is possible or not to assign those values to the unknowns in order for the expression to evaluate to the given result. The particular assignment of values does not matter to John, as he wants to do it by himself. He only wants to know whether it is possible or not.
Input
Each test case in the input le consists of two lines:
The fi rst line contains a sequence of natural numbers. The first one (1<=n<=5) is the number of unknowns that will occur in the expression. It is followed by a sequence of n integers v 1 ... v n (0<=v i<=50), which are the values to be assigned to the unknowns. Finally, there is an integer m (0<=m<=1000) representing the desired result of the evaluation of the expression.
The second line contains an arithmetic expression composed of lowercase letters (a-z), brackets (( and )) and binary operators (+, -, *). This expression will contain n unknowns, represented by n di fferent lowercase letters, without repetitions. The expression will not contain any blanks and will always be syntactically correct, i.e. it is just an unknown or has the form (e1 op e2 ), where e1 and e2 are expressions and op is one of the three possible binary operators.
The input will finish with a dummy test case of just one line containing 0 0, which must not be processed.
The fi rst line contains a sequence of natural numbers. The first one (1<=n<=5) is the number of unknowns that will occur in the expression. It is followed by a sequence of n integers v 1 ... v n (0<=v i<=50), which are the values to be assigned to the unknowns. Finally, there is an integer m (0<=m<=1000) representing the desired result of the evaluation of the expression.
The second line contains an arithmetic expression composed of lowercase letters (a-z), brackets (( and )) and binary operators (+, -, *). This expression will contain n unknowns, represented by n di fferent lowercase letters, without repetitions. The expression will not contain any blanks and will always be syntactically correct, i.e. it is just an unknown or has the form (e1 op e2 ), where e1 and e2 are expressions and op is one of the three possible binary operators.
The input will finish with a dummy test case of just one line containing 0 0, which must not be processed.
Output
For each test case, print a single line with YES if there exists an assignment of the values v
1 . . . v
n to the unknowns such that the expression evaluates to m, and NO otherwise. Notice that each value v
i must be assigned to exactly one unknown.
Sample Input
3 2 3 4 14 ((a+b)*c) 2 4 3 11 (a-b) 1 2 2 a 0 0
Sample Output
YES NO YES 简单版的表达式求值 为什么说是简单版的呢= = 因为题目隐含条件是每次运算都要打上括号。。然后就不用运算符比较大小了 再用一个next_permutation函数 来枚举所有情况即可 注意要用do while 直接用while 会忽略第一个序列。。。。 代码:#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stack> using namespace std; int num[55]; char str[111]; int q[55]; int m,n,len; int operate(int a,char op,int b) { if(op=='+') return a+b; if(op=='-') return a-b; if(op=='*') return a*b; } int work() { stack<int>opnd; stack<char>optr; int i,j=0; for(i=0;i<len;i++) { if(str[i]!=')') { if(str[i]!='+'&&str[i]!='-'&&str[i]!='*'&&str[i]!='(') opnd.push(q[j++]); else optr.push(str[i]); } else { int a,b; char op; b=opnd.top(); opnd.pop(); a=opnd.top(); opnd.pop(); op=optr.top(); optr.pop(); optr.pop(); // cout<<a<<' '<<op<<' '<<b<<endl; opnd.push(operate(a,op,b)); } } return opnd.top(); } int main() { int i; int tt; while(~scanf("%d%d",&m,&q[0])) { if(m==0&&q[0]==0) break; tt=0; for(i=1;i<m;i++) scanf("%d",&q[i]); sort(q,q+m); scanf("%d",&n); scanf("%s",str); if(m==1&&q[0]==n) { cout<<"YES"<<endl; continue; } len=strlen(str); do{ int ans=work(); if(ans==n) { tt=1; break; } }while(next_permutation(q,q+m)); if(tt==1) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }