Equivalent expression (noip2005)

Source: Internet
Author: User

3. equivalent expression

[Problem description]
On the day of the Mid-Autumn Festival, representatives of the mathematics class gave you a Multi-choice question about algebra expressions. In the stem of this question, an algebraic expression is first given, and several options are listed. Each option is also an algebraic expression, the requirement of the question is to determine which algebra expressions in the option are equivalent to the expressions in the question stem.
This question is very difficult, because mathematics classes are very interested in computer programming, so he wondered if he could use a computer to solve the problem. If you are a representative of a mathematics class, can you complete this task?

Each expression in this multiple-choice question meets the following attributes:

1. The expression may only contain one variable 'A '.

2. The numbers in the expressions are all positive integers and all are smaller than 10000.
3. the expression can contain four types of operations: '+' (plus), '-' (minus), '*' (multiplication), and '^' (multiplication power ), and parentheses '(',')'. Parentheses have the highest priority, followed by '^', followed by '*', followed by '+' and '-'. '+' And '-' have the same priority. Operations with the same priority are performed from left to right. (Note: The operators '+', '-', '*', '^', and parentheses '(', ')' are all English characters)

4. The power index can only be a positive integer between 1 and 10 (including 1 and 10 ).

5. There may be extra spaces in the header or tail of the expression.

The following is an example of a rational expression:

(A ^ 1) ^ 2) ^ 3, A * A + A-A, (a + a), 9999 + (a-a) *, 1 + (A-1) ^ 3, 1 ^ 10 ^ 9 ......
[Input file]

The first line shows the expression in the question stem. The second row is an integer N (2 <=n <= 26), indicating the number of options. Next n rows. Each row contains an option expression. The numbers of the N options are a, B, c, d ......

The length of the expression in the input cannot exceed 50 characters, and the expression in the option is always equivalent to that in the question stem.

[Output file]
The output file contains one line. This line includes a series of option labels, indicating which options are equivalent to the expressions in the question stem. The options are listed alphabetically without spaces.
[Example input]
(A + 1) ^ 2
3
(A-1) ^ 2 + 4 *
A + 1 +
A ^ 2 + 2 * a * 1 + 1 ^ 2 + 10-10 + A-
[Sample output]
AC
[Data scale]
For 30% of the data, only two operators '+' and '-' may appear in the expression '-';
For other data, the four operators '+', '-', '*', and '^' may all appear in the expression.
For all the data, parentheses '(' and ')' may appear in the expression ')'.

Resolution:

(1) If it is difficult to directly judge the equivalence of the two expressions, I have not figured out how to compare them. However, we can use the algebra method to substitute different A values into different types and quickly exclude expressions with different results. What we leave is equivalent. (Note: it is best to randomly generate the value here and generate multiple groups. This ensures that the value is safe ).

(2) expression evaluate: There are two methods: one is to replace the suffix and then evaluate the value; the other is to directly evaluate the value of the infix expression. I like the second method.

The infix expression is directly evaluated:

First, all operators and parentheses in the expression are specified according to the operation rules. The level of each operator in the expression can be obtained based on the specified level. Then recursive solution:

(1) Find the lowest-level operator OPT and divide the expression into the left and right sides;

(2) Evaluate the S0 value of the expression on the left of OPT;

(3) evaluate the expression S1 on the left of OPT;

(4) S0 opt S1.

Operator level:

Symbol

+-

*/

^

(

)

Priority

1

2

3

3

-3

{Infix expression evaluate directly: each operator is assigned a different level based on the operation rules. During the operation, find the lowest-level operator in the expression and divide the expression into the left and right parts. First, find the left side and then the right side, finally, calculate the result of the entire expression .} VaR A: array [0 .. 30] of string; F: array [0 .. 30] of Boolean; B: array [1 .. 4] of integer; E: array [0 .. 30, 1 .. 4] of int64; H: array [1 .. 500] of integer; N: integer; procedure level (S: string); var I, Len, base: integer; begin Len: = length (s); for I: = 1 to Len do h [I]: = maxint; base: = 0; for I: = 1 to Len do case s [I] of '(': Inc (base, 3); ')': Dec (base, 3); '+', '-': H [I]: = base + 1 ;'*': H [I]: = base + 2; '^': H [I]: = base + 3; end; Procedure Init; var I: integer; begin assign (input, 'equal. in '); reset (input); readln (A [0]); readln (n); for I: = 1 to n do readln (A [I]); close (input); end; function data (L, R, x, t: integer): integer; var I: longint; begin while (A [T] [l] = '(') or (A [T] [l] = '') Do Inc (L ); while (A [T] [r] = ') or (A [T] [r] = '') Do Dec (r); Data: = 0; if a [T] [l] = 'A' then exit (x); for I: = L to r do data: = Data * 10 + ord (A [T] [I])-48; end; function find (L, R: integer): int64; var I, Min: integer; begin min: = maxint; find: = 0; for I: = r downto l do if H [I] <min then begin min: = H [I]; find: = I; end; function OPT (u, K, V: int64; T: integer): int64; var I: integer; begin case a [T] [k] of '+': OPT: = u + V; '-': OPT: = u-V; '*': OPT: = u * V; '^': Begin OPT: = 1; for I: = 1 to V do opt: = opt * U; end; function work (L, R, X, I: integer): int64; var K, U, V: int64; begin K: = find (L, R ); if K = 0 Then exit (data (L, R, X, I); U: = work (L, K-1, X, I); V: = work (k + 1, R, X, I); Work: = OPT (u, K, V, I); end; Procedure main; var I, j, Len: integer; Bo: Boolean; begin randomize; for I: = 1 to 4 do B [I]: = random (1000); for I: = 0 to n do begin level (a [I]); Len: = length (A [I]); For J: = 1 to 4 do e [I, j]: = work (1, Len, B [J], I); If I> 0 then begin Bo: = true; for J: = 1 to 4 do if E [I, j] <> E [0, J] Then Bo: = false; if Bo then write (CHR (64 + I); end; begin assign (output, 'equal. out'); rewrite (output); Init; main; close (output); end.
View code

 

Equivalent expression (noip2005)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.