Problem K Stack

Source: Internet
Author: User

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




  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) (a) > (b)? (a): (b))
  13. #define MIN2 (a) (a) < (b)? (a): (b))
  14. Using namespace std;
  15. map<Char,int>m;
  16. String transform (string s) //conversion to suffix expression
  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]=' + ';
  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. }

Problem K Stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.