Description
A math instructor is too lazy to grade A question in the exam papers in which students be supposed to produce a complicat Ed 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 can help.
You is to write a program to read different formulas and determine whether or not they is arithmetically equivalent.
Input
The first line of the input contains a integer n (1 <= n <=) That's the number of the test cases. Following the first line, there is both lines for each test case. A test case consists of the arithmetic expressions, each of the separate line with the most characters. There is no blank line in the input. An expression contains one or more of the following:
- variables (case insensitive).
- Single digit numbers.
- Matched left and right parentheses.
- Binary operators +,-and * which is used for addition, subtraction and multiplication respectively.
- Arbitrary number of blank or tab characters between above tokens.
Note:expressions is syntactically correct and evaluated from left to right with equal precedence (priority) for all Oper Ators. The coefficients and exponents of the variables is 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 is arithmetically equivalent, "YES", otherwise "NO" must be printed as the output of the program. Output should is all in upper-case characters.
Sample Input
3 (A+B-C) * * (A+a) + (b*2)-(3*c) +ca*2-(a+c) + ((a+c+e) * *) 3*a+c+ (2*e) (A-B) * (A-B) (A*a)-(2*a*b)-(B*B)
Sample Output
Yesyesno
- #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) (a) > (b)? (a): (b))
- #define MIN2 (a) (a) < (b)? (a): (b))
- Using namespace std;
- map<Char,int>m;
- String transform (string s) //conversion to suffix expression
- {
- 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]=' + ';
- 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;
- }
Problem K Stack