Equals are Equals
Time limit2sMemory limit512 MB
Parse multivariate polynomial expressions with integer coefficients and decide whether each student answer is equivalent to the reference expression.
- Level
Medium7 of 10
- Topics
- String, Implementation, Recursion, Math
- Solved
- No attempts yet
Problem
Mr. Simpson got up feeling slightly tired. Another day of hard work was starting. A pile of papers was waiting for his inspection on the desk in his office. The papers held his students' answers to the questions in his math class, but the answers looked like nothing more than stains of ink.
His headache comes from the "creativity" of his students. They gave him a variety of ways to answer each problem. He has his own answer to each problem, which is correct, of course, and the best from his aesthetic point of view.
Some students wrote algebraic expressions equivalent to the expected answer, but many of them look quite different from Mr. Simpson's answer in their literal form. Some wrote algebraic expressions that are not equivalent to his answer, yet look very similar to it. Only a few answers were exactly the same as his.
It is his duty to check whether each expression is mathematically equivalent to the answer he prepared. This prevents an expression that is equivalent to his own from being marked "incorrect", even if it offends his aesthetic standards.
After five days of checking expressions he suddenly stood up and yelled, "I've had enough! I must call for help."
Write a program that helps Mr. Simpson judge whether each answer is equivalent to the "correct" one. The algebraic expressions on the papers are multivariate polynomials over the variable symbols , , ..., with integer coefficients, for example , and .
Mr. Simpson inputs every answer expression as it is written on the paper. He promises that every expression he inputs follows these rules. An expression is a sequence of terms separated by the additive operators + and -, and it represents the sum of the terms with those operators. A term is a juxtaposition of multiplicands and represents their product. A multiplicand is one of the following: (a) a non-negative integer written as a sequence of decimal digits, (b) a variable symbol (one of the lowercase letters a to z), possibly followed by the symbol ^ and one non-zero digit, which is the power of that variable, or (c) a parenthesized algebraic expression, recursively. The operators + and - appear only as binary operators, never as unary operators that give the sign of an operand.
He says that he puts one or more space characters before an integer when it immediately follows another integer or a digit that follows the symbol ^. He may also put spaces here and there in an expression to make it readable, but he never puts a space between two consecutive digits of one integer. He remarks that the expressions are not complicated: if every - in an expression is replaced with + and the result is fully expanded into a sum of products of coefficients and powered variables, no variable is raised to its 10th power and no coefficient exceeds one billion.
Input
The input is a sequence of blocks of lines. A block consists of lines, each containing one expression, followed by a terminating line. After the last block there is one more terminating line. A terminating line consists solely of a period ..
The first expression of a block is the one Mr. Simpson prepared. All the expressions that follow in the block are answers by the students. An expression consists of lowercase letters, digits, the operators +, - and ^, the parentheses ( and ), and spaces. A line containing an expression has at most 80 characters.
Output
For each answer by a student, print a line consisting solely of yes if the answer is mathematically equivalent to the expected answer, and no otherwise. After each block, print a line consisting solely of a period ..