poj 1686

来源:互联网 发布:mac复制文件夹到u盘 编辑:程序博客网 时间:2024/06/07 03:43

传送门

Lazy Math Instructor
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3993 Accepted: 1393

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)+ca*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

YESYESNO

Source

Tehran 2000


比较上下的字符串化简之后是不是一样的,如果是一样的就输出YES 不是就输出NO 中间可能有多个空格


#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <cstdio>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>using namespace std;#define pi acos(-1)#define endl '\n'#define rand() srand(time(0));#define me(x) memset(x,0,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0);typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;//const int dx[]={-1,0,1,0,-1,-1,1,1};//const int dy[]={0,1,0,-1,1,-1,1,-1};const int maxn=1e3+5;const int maxx=1e6+100;const double EPS=1e-7;const int MOD=1000000007;#define mod(x) ((x)%MOD);template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;/*lch[root] = build(L1,p-1,L2+1,L2+cnt);    rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*//*lch[root] = build(L1,p-1,L2,L2+cnt-1);    rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}int len,ix;char s[81];void space(){    while(s[ix+1]==9||s[ix+1]==32) ++ix;}int solve(int x){    if(x<58) x-=48;    return x;}int cal(){    int r=0,a,b,c,flag=1;    space();    a=s[++ix]=='('?cal():solve(s[ix]);//遇到左括号就递归一次    while(ix<len-1)    {        space();        b=s[++ix];        if(b==')')        {            if(flag)                r+=a;            else r-=a;            return r;        }        space();        c=s[++ix]=='('?cal():solve(s[ix]);        if(b=='*')            a*=c;        else        {            if(flag)                r+=a;            else r-=a;            flag=b-('*'+1)?0:1;            a=c;        }    }    if(flag) r+=a;    else r-=a;    return r;}int main(){    int t,a,b;    cin>>t;    getchar();    while(t--)    {        gets(s);        len=strlen(s);        ix=-1;a=cal();        gets(s);        len=strlen(s);        ix=-1;b=cal();        if(a==b)            puts("YES");        else puts("NO");    }}


0 0
原创粉丝点击