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. }

Problem K 栈的更多相关文章

  1. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  2. Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]

    题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...

  3. Gym 101981K - Kangaroo Puzzle - [玄学][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem K]

    题目链接:http://codeforces.com/gym/101981/problem/K Your friend has made a computer video game called “K ...

  4. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题

    Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...

  5. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem K. KMC Attacks 交互题 暴力

    Problem K. KMC Attacks 题目连接: http://codeforces.com/gym/100714 Description Warrant VI is a remote pla ...

  6. XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking

    题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...

  7. 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...

  8. HDU 6342.Problem K. Expression in Memories-模拟-巴科斯范式填充 (2018 Multi-University Training Contest 4 1011)

    6342.Problem K. Expression in Memories 这个题就是把?变成其他的使得多项式成立并且没有前导零 官方题解: 没意思,好想咸鱼,直接贴一篇别人的博客,写的很好,比我的 ...

  9. 华农oj Problem K: 负2进制【有技巧构造/待补】

    Problem K: 负2进制 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 51 Solved: 6 [Submit][Status][Web Boa ...

随机推荐

  1. oracle 生成随机数【待整理】

    http://www.cnblogs.com/ulex/p/4415478.html http://blog.sina.com.cn/s/blog_6a01140c0100wimi.html

  2. JavaScript的事件对象_其他属性和方法

    在标准的 DOM 事件中,event 对象包含与创建它的特定事件有关的属性和方法.触发的事件类型不一样,可用的属性和方法也不一样. 在这里,我们只看所有浏览器都兼容的属性或方法.首先第一个我们了解一下 ...

  3. UAT 环境

    User Accept Environment 用户仿真测试环境

  4. [转]Android ORM系列之GreenDao最佳实践

    GreenDAO是一个可以帮助Android开发者快速将Java对象映射到SQLite数据库的表单中的ORM解决方案,通过使用一个简单的面向对象API,开发者可以对Java对象进行存储.更新.删除和查 ...

  5. imeOptions 属性详解

    默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘 设置 android:imeOptions="actionDone" ,软键盘下方变成“完成”,点击后光 ...

  6. Unity5.3官方VR教程重磅登场-系列2

    作者:王寒链接:https://zhuanlan.zhihu.com/p/20485529来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 欢迎继续我们的学习. 北京时间 ...

  7. virtualbox macosx10.9改变分辨率方法

    VBoxManage setextradata "osx10.9" VBoxInternal2/EfiGopMode 5 VBoxManage setextradata " ...

  8. java 对象 Serializable注意事项

    在序列化时,有几点要注意的: 1:当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量. 2:如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存. ...

  9. WLAN频段介绍-04

    ISM频段 ISM频段,此频段主要是开放给工业.科学.医学三个主要机构使用,该频段是依据美国联邦通讯委员会(FCC)所定义出来,并没有所谓使用授权的限制. 工业频段:美国频段为902-928MHz,欧 ...

  10. Remove Duplicates from Sorted List II [LeetCode]

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...