等价表达式
来源:互联网 发布:wind python高频数据库 编辑:程序博客网 时间:2024/05/17 22:58
1686:等价表达式
- 查看
- 提交
- 统计
- 提示
- 提问
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 判断两个表达式在数学上是否是等价的。
- 输入
- 第一行:N(1<=N<=20),表示测试数据组数。
接下来每组测试数据包括两行,每行包括一个数学表达式,每个表达式的长度不超过80个字符。输入数据没有空行。
一个表达式可能包括:
单个英文字母表示的变量(区分大小写)
数字(只有一位数)
配对的括号
运算符加+、减-、乘*
任意数量的空格或tab(可能出现在表达式中间的任何位置)
注意:表达式保证是语法正确的,且所有运算符的优先级相同,运算次序从左至右。变量的系数和指数保证不超过16位整数。 - 输出
- 对每个测试数据,输出一行:等价则输出“YES”,不等价则输出“NO”。
- 样例输入
3(a+b-c)*2(a+a)+(b*2)-(3*c)+ca*2-(a+c)+((a+c+e)*2)3*a+c+(2*e)(a-b)*(a-b)(a*a)-(2*a*b)-(b*b)
- 样例输出
YESYES
NO
个人理解:参考百度答案
#include <cstdio>#include <iostream>#include <string>#include <cstring>#include <stack>using namespace std;#define MAX 100string change(char *p) {string cnt;int len=strlen(p);stack<char>astack;for(int i=0; i<=len; i++) {if((p[i]>='0' && p[i]<='9') || (p[i]>='a' && p[i]<='z'))cnt+=p[i];else if(p[i]=='(')astack.push('(');else if(p[i]==')') {while(astack.top()!='(') {cnt+=astack.top();astack.pop();}astack.pop();}else if(p[i]=='+' || p[i]=='-') {while(!astack.empty() && astack.top()!='(') {cnt+=astack.top();astack.pop();}astack.push(p[i]);}else if(p[i]=='*') {while(!astack.empty() && astack.top()!='(' && astack.top()=='*') {cnt+=astack.top();astack.pop();}astack.push(p[i]);}else if(p[i]==' ' || p[i]=='')continue;}while(!astack.empty()) {cnt+=astack.top();astack.pop();}return cnt;}int getsum(string num) {int len=num.length();stack<int>astack;for(int i=0; i<len; i++) {if(num[i]<='z' && num[i]>='a') {int ascii=num[i];astack.push(ascii);}else if(num[i]>='0' && num[i]<='9')astack.push((int)num[i]-'0');else if(num[i]=='+') {int n=astack.top();astack.pop();int m=astack.top();astack.pop();m+=n;astack.push(m);}else if(num[i]=='*') {int n=astack.top();astack.pop();int m=astack.top();astack.pop();n*=m;astack.push(n);}else if(num[i]=='-') {int n=astack.top();astack.pop();int m=astack.top();astack.pop();m-=n;astack.push(m);}}int result=astack.top();astack.pop();return result;}int main() {int N;scanf("%d", &N);cin.get();while(N--) {char s1[MAX], s2[MAX];cin.getline(s1, MAX);string p1=change(s1);int a=getsum(p1);cin.getline(s2, MAX);string p2=change(s2);int b=getsum(p2);if(a==b)cout << "YES" << endl;elsecout << "NO" << endl;}return 0;}
阅读全文
0 0
- 等价表达式
- 等价表达式
- 等价表达式
- 等价表达式
- 等价表达式
- sicily 1149 等价表达式
- Sicily 1149. 等价表达式
- vijos-1003等价表达式
- Sicily 1149. 等价表达式
- NOIP2005 等价表达式
- NOIP2005 等价表达式
- noip的等价表达式
- NOIP 2005 等价表达式
- 洛谷 P1054 等价表达式
- NOIP2005【等价表达式】
- NOIP 2005 等价表达式
- NOIP2005 等价表达式
- 正规表达式是否等价
- python中Groupby使用(四)-通过函数进行分组
- 1686
- Linux系统下安装rz/sz命令的使用
- 操作系统中地址变换
- 19-Eclipse 自动格式化,打印日志到文本文件,方法被引用
- 等价表达式
- 导弹是怎样做成的
- IOS应用间跳转
- hue在centos7下的安装
- httpclient封装获取响应实体和接口信息的方法
- Behavior
- 归并排序
- Linux上面部署java项目
- 红黑树