UESTC Training for Data Structures——K
来源:互联网 发布:mac文稿占用 编辑:程序博客网 时间:2024/05/22 10:50
Problem K
Problem 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.
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.
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
/*表达式求值,我把每个 'a'--'z' 的字母分别hash成0--25 再把每个 'A'--'Z' 的字母分别hash成0--25*/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#define N 100#define INF 100000000int calculate(char s[],int st,int en) //计算{ if(en<st) return 0; if(en==st) { if(s[st]>='A' && s[st]<='Z') return (s[st]-'A'); else if(s[st]>='a' && s[st]<='z') return (s[st]-'a'); else return s[st]-'0'; } int p1=-1,p2=-1; //p1 : '+' 或者是 '-'的位子;p2 : '*'的位子 int kuohao=0; for(int i=st;i<=en;i++) { if(s[i]=='(') kuohao++; else if(s[i]==')') kuohao--; else if((s[i]=='+' || s[i]=='-') && kuohao==0) p1=i; else if(s[i]=='*' && kuohao==0) p2=i; } if(p1<0 && p2<0) return calculate(s,st+1,en-1); if(p1>0) { if(s[p1]=='+') return calculate(s,st,p1-1)+calculate(s,p1+1,en); else return calculate(s,st,p1-1)-calculate(s,p1+1,en); } else return calculate(s,st,p2-1)*calculate(s,p2+1,en);}void manage(char s[]){ char ans[N],*p1=ans; char *p=s; while(*p!='\0') { while(((*p==' ') ||(*p=='\t')) && (*p!='\0')) *p++; *p1=*p; p1++; p++; } *p1='\0'; strcpy(s,ans);}int main(){ int t; scanf("%d",&t);getchar(); while(t--) { char s1[N],s2[N]; gets(s1); gets(s2); manage(s1),manage(s2); int ans1=calculate(s1,0,strlen(s1)-1); int ans2=calculate(s2,0,strlen(s2)-1); if(ans1==ans2) printf("YES\n"); else printf("NO\n"); } return 0;}
- UESTC Training for Data Structures——K
- UESTC Training for Data Structures——A
- UESTC Training for Data Structures——B
- UESTC Training for Data Structures——C
- UESTC Training for Data Structures——D
- UESTC Training for Data Structures——E
- UESTC Training for Data Structures——F
- UESTC Training for Data Structures——G
- UESTC Training for Data Structures——H
- UESTC Training for Data Structures——I
- UESTC Training for Data Structures——J
- UESTC Training for Data Structures——L
- UESTC Training for Data Structures——M
- 2014 UESTC Training for Data Structures—— E - 休生伤杜景死惊开
- 【UESTC】2015 UESTC Training for Data Structures
- UESTC Training for Data Structures(总结)
- 2016 UESTC Training for Data Structures
- 2016 UESTC Training for Data Structures
- UESTC Training for Data Structures——J
- Oracle 游标使用全解
- 10条编程语录(很有意义)
- 千万级pv高性能高并发网站架构与设计
- 一个简单的异或加密算法
- UESTC Training for Data Structures——K
- 堆和栈(明确的说明了计算机堆和栈的区别,令我受益很多)
- Ubuntu删除自己编译的内核
- UESTC Training for Data Structures——L
- Oracle存储过程返回游标
- cocos2d-x 读取资源导致当机问题
- UESTC Training for Data Structures——M
- UVa 试题 10115
- Oracle游标使用大全