Description
A math instructor is too lazy to grade a question in the exam papers in which students are supposed to produce a complicated formula for the question asked. Students may write correct answers in different forms which makes grading very hard. So, the instructor needs help from computer programmers and you can help.
You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.
Input
The first line of the input contains an integer N (1 <= N <= 20) that is the number of test cases. Following the first line, there are two lines for each test case. A test case consists of two arithmetic expressions, each on a separate line with at most 80 characters. There is no blank line in the input. An expression contains one or more of the following:
- Single letter variables (case insensitive).
- Single digit numbers.
- Matched left and right parentheses.
- Binary operators +, - and * which are used for addition, subtraction and multiplication respectively.
- Arbitrary number of blank or tab characters between above tokens.
Note: Expressions are syntactically correct and evaluated from left to right with equal precedence (priority) for all operators. The coefficients and exponents of the variables are guaranteed to fit in 16-bit integers.
Output
Your program must produce one line for each test case. If input expressions for each test data are arithmetically equivalent, "YES", otherwise "NO" must be printed as the output of the program. Output should be all in upper-case characters.
Sample Input
3 (a+b-c)*2 (a+a)+(b*2)-(3*c)+c a*2-(a+c)+((a+c+e)*2) 3*a+c+(2*e) (a-b)*(a-b) (a*a)-(2*a*b)-(b*b)
Sample Output
YES YES NO
- #include <cstring>
- #include <string>
- #include <cstdio>
- #include <algorithm>
- #include <queue>
- #include <cmath>
- #include <vector>
- #include <cstdlib>
- #include <iostream>
- #include <stack>
- #include <map>
- #define max2(a,b) ((a) > (b) ? (a) : (b))
- #define min2(a,b) ((a) < (b) ? (a) : (b))
- using namespace std;
- map<char,int>m;
- string transform(string s) //转化为后缀表达式
- {
- int len=s.length();
- char c[100];
- int top=0;
- stack<char>exp;
- for(int i=0;i<len;i++)
- {
- if(isalnum(s[i])) c[top++]=s[i];
- else
- {
- switch(s[i])
- {
- case ‘(‘:
- exp.push(s[i]);
- break;
- case ‘)‘:
- while(exp.top()!=‘(‘)
- {
- c[top++]=exp.top();
- exp.pop();
- }
- exp.pop();
- break;
- case ‘+‘:
- case ‘-‘:
- case ‘*‘:
- while(!exp.empty()&&m[s[i]]<=m[exp.top()])
- {
- c[top++]=exp.top();
- exp.pop();
- }
- exp.push(s[i]);
- }
- }
- }
- while(!exp.empty())
- {
- c[top++]=exp.top();
- exp.pop();
- }
- c[top]=‘\0‘;
- string temp=c;
- return temp;
- }
- int cal(string s)
- {
- int len=s.length();
- stack<int>c;
- for(int i=0;i<len;i++)
- {
- if(isalnum(s[i]))
- {
- if(isdigit(s[i]))
- c.push(s[i]-‘0‘);
- else
- c.push(s[i]);
- }
- else
- {
- int a=c.top();
- c.pop();
- int b=c.top();
- c.pop();
- switch(s[i])
- {
- case ‘+‘:c.push(b+a);
- break;
- case ‘-‘:c.push(b-a);
- break;
- case ‘*‘:c.push(b*a);
- }
- }
- }
- return c.top();
- }
- int main()
- {
- int t;
- string s1,s2;
- m[‘(‘]=0;
- m[‘+‘]=m[‘-‘]=1;
- m[‘*‘]=2;
- cin>>t;
- getchar();
- while(t--)
- {
- getline(cin,s1);
- getline(cin,s2);
- string t1=transform(s1);
- string t2=transform(s2);
- int ans1=cal(t1);
- int ans2=cal(t2);
- if(ans1==ans2)
- cout<<"YES"<<endl;
- else
- cout<<"NO"<<endl;
- }
- return 0;
- }