Problem K 栈

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