Problem K 栈

时间:2022-07-15 15:06:37

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
  1. #include <cstring>
  2. #include <string>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <cmath>
  7. #include <vector>
  8. #include <cstdlib>
  9. #include <iostream>
  10. #include <stack>
  11. #include <map>
  12. #define max2(a,b) ((a) > (b) ? (a) : (b))
  13. #define min2(a,b) ((a) < (b) ? (a) : (b))
  14. using namespace std;
  15. map<char,int>m;
  16. string transform(string s)    //转化为后缀表达式
  17. {
  18. int len=s.length();
  19. char c[100];
  20. int top=0;
  21. stack<char>exp;
  22. for(int i=0;i<len;i++)
  23. {
  24. if(isalnum(s[i])) c[top++]=s[i];
  25. else
  26. {
  27. switch(s[i])
  28. {
  29. case '(':
  30. exp.push(s[i]);
  31. break;
  32. case ')':
  33. while(exp.top()!='(')
  34. {
  35. c[top++]=exp.top();
  36. exp.pop();
  37. }
  38. exp.pop();
  39. break;
  40. case '+':
  41. case '-':
  42. case '*':
  43. while(!exp.empty()&&m[s[i]]<=m[exp.top()])
  44. {
  45. c[top++]=exp.top();
  46. exp.pop();
  47. }
  48. exp.push(s[i]);
  49. }
  50. }
  51. }
  52. while(!exp.empty())
  53. {
  54. c[top++]=exp.top();
  55. exp.pop();
  56. }
  57. c[top]='\0';
  58. string temp=c;
  59. return temp;
  60. }
  61. int cal(string s)
  62. {
  63. int len=s.length();
  64. stack<int>c;
  65. for(int i=0;i<len;i++)
  66. {
  67. if(isalnum(s[i]))
  68. {
  69. if(isdigit(s[i]))
  70. c.push(s[i]-'0');
  71. else
  72. c.push(s[i]);
  73. }
  74. else
  75. {
  76. int a=c.top();
  77. c.pop();
  78. int b=c.top();
  79. c.pop();
  80. switch(s[i])
  81. {
  82. case '+':c.push(b+a);
  83. break;
  84. case '-':c.push(b-a);
  85. break;
  86. case '*':c.push(b*a);
  87. }
  88. }
  89. }
  90. return c.top();
  91. }
  92. int main()
  93. {
  94. int t;
  95. string s1,s2;
  96. m['(']=0;
  97. m['+']=m['-']=1;
  98. m['*']=2;
  99. cin>>t;
  100. getchar();
  101. while(t--)
  102. {
  103. getline(cin,s1);
  104. getline(cin,s2);
  105. string t1=transform(s1);
  106. string t2=transform(s2);
  107. int ans1=cal(t1);
  108. int ans2=cal(t2);
  109. if(ans1==ans2)
  110. cout<<"YES"<<endl;
  111. else
  112. cout<<"NO"<<endl;
  113. }
  114. return 0;
  115. }