# | Problem | Pass Rate (passed user / total user) |
---|---|---|
10966 | Infix to syntax tree |
|
10968 | Prefix to infix |
|
10972 | Remove unnecessary parentheses |
|
12183 | Fairy Testing |
|
12193 | Binary Search Tree Operation |
|
13433 | The password |
|
13435 | Longest path on a tree |
|
13439 | Prefix Expression Evaluation |
|
Description
Given an infix Boolean expression with parentheses, which has at most 4 variables ‘A’, ’B’, ‘C’, and ‘D’, and two operators ‘&’ and ‘|’. Build a corresponding syntax tree for it.
To parse the infix expression with parentheses, use the grammar below.
EXPR = FACTOR | EXPR OP FACTOR
FACTOR = ID | (EXPR)
EXPR is the expression, ID is one of ‘A’, ‘B’, ’C’, or ‘D’, and OP is one of ‘&’ or ‘|’.
You will be provided with main.c and function.h. main.c contains the implementation of functions printPrefix and freeTree. function.h contains the definition of tree node and variables. You only need to implement the following three functions in function.c.
BTNode* makeNode(char c) // create a node without any child
BTNode* EXPR() // parse an infix expression and generate a syntax tree
BTNode* FACTOR() // use the parse grammar FACTOR = ID | (EXPR) to deal with parentheses
For OJ submission:
Step 1. Submit only your function.c into the submission block.(Please choose C compiler)
Step 2. Check the results and debug your program if necessary.
Input
The input contains N infix expressions, which has at most 4 variables ‘A’, ’B’, ‘C’, and ‘D’, two operators ‘&’ and ‘|’, and parentheses. All parentheses are matched.
Output
The output contains N prefix expressions without parentheses, which are preorders of syntax trees.
Sample Input Download
Sample Output Download
Partial Judge Code
10966.cPartial Judge Header
10966.hTags
Discuss
Description
Given an prefix Boolean expression, which has at most 4 variables ‘A’, ’B’, ‘C’, and ‘D’, and two operators ‘&’ and ‘|’. Output its infix presentation with necessary parenthesis.
- Ex: input: ||A&BCD
output: A|(B&C)|D
Hint : About necessary parenthesis, you can observe the syntax tree which has been constructed, then you will find out the rule. For example, the infix expression of |&|AB&CDA with necessary parenthesis is A|B&(C&D)|A, rather than (A|B)&(C&D)|A .
- Syntax tree of |&|AB&CDA
You will be provided with main.c and function.h, and asked to implement function.c.
For OJ submission:
Step 1. Submit only your function.c into the submission block.(Please choose C compiler)
Step 2. Check the results and debug your program if necessary.
Input
The first line will have a number N with the number of test cases, followed by N lines of input, each contain the prefix Boolean expression.
Output
There are N lines infix expression with necessary parenthesis.
Sample Input Download
Sample Output Download
Partial Judge Code
10968.cPartial Judge Header
10968.hTags
Discuss
Description
Given an infix Boolean expression with parentheses, which has at most 4 variables ‘A’, ’B’, ‘C’, and ‘D’, and two operators ‘&’ and ‘|’. Please remove unnecessary parentheses and print the infix expression. Existence of unnecessary parentheses doesn’t affect the result of expression. For example,
(A&B)|(C&D) → A&B|(C&D)
(((A|B))) → A|B
Hint: You can combine two homework. Build a syntax tree and print the infix expression with necessary parentheses.
For OJ submission:
Step 1. Submit your main.c into the submission block.(Please choose C compiler)
Step 2. Check the results and debug your program if necessary.
Input
The input is an infix expression, which has at most 4 variables ‘A’, ’B’, ‘C’, and ‘D’, two operators ‘&’ and ‘|’, and parentheses. The length of the infix expression is less than 256.
Output
The output is an infix expression without unnecessary parentheses.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
In this problem, you are given a prefix Boolean expression, which has at most N=10^5 variables and 2 kinds of operators ‘&’ and ‘|’. Each variable has an ID number. The values of these variables can be either 0 or 1, and are all initialized as 0.
You will be given M ID numbers. For each ID number, you should flip (0 will become 1, 1 will become 0) the value of the corresponding variable and then compute/output the result of the prefix Boolean expression.
Example:
Assume the prefix Boolean expression is &[1][2] and the given ID numbers are “2 1”. First the value of variable 2 will change from 0 to 1. The output of &[1][2] is 0&1=0. Next, the value of variable 1 will change from 0 to 1. The output of &[1][2] is 1&1=1.
Hint: (You can go to the Input/Output description directly, and come back for hints if necessary.)
Hint 1: To solve this problem, you can parse the input and derive the output recursively each time a variable is flipped. Unfortunately, this brute-force approach is not efficient and can lead to TLE, in particular the 3rd and 4th testcases in this problem. We encourage you to use the syntax tree of the prefix Boolean expression for better efficiency.
Hint 2: The key idea behind the syntax-tree approach is to avoid re-evaluation of the whole expression every time a bit is flipped. Some more hints are provided for you as follows:
- [Data structure] Use a tree node pointer array to record the address of each variable (leaf) node of the syntax tree: you can locate each variable to be flipped directly;
- [Data structure] For each tree node i, record the value of its subtree (rooted at node i);
- [Algorithm] Starting from the variable node to be flipped, move upward following the syntax tree and update the node values until the output of the syntax tree (or the Boolean expression) can be determined.
Example:
Consider the following syntax tree for "|&|[1][2][3][4]". Assume [1] is flipped. You will find that the value of node A should be changed to 1|0=1. Moreover, you note that the value of B remains unchanged, i.e., 1&0=0, and the checking process can stop.
Later, when [3] is flipped, both the values of node B and the root node should be changed. In this case, the output is also changed.
Sample structure:
typedef struct node{
int value; // This is the value of the subtree, not the ID number
int tokentype;
struct node *leftchild;
struct node *rightchild;
struct node *parent; //pointing to the parent node
}Node;
Node* variable[100001]; // This array stores the pointers pointing to the tree nodes. For example, variable[0] points to the node with ID number 0.
Input
The first line has one number T, 0<T<= 7, indicating the number of testcases.
The following lines are the inputs of the testcases.
Each testcase contains three lines:
1. N, M (0<N<=100000, indicating the number of variables in a given prefix Boolean expression. 0<M<=100000, indicating how many times the values are flipped).
2. A string S, describing the prefix Boolean expression (& means and gate, | means or gate, [i] means a variable ID)
3. M numbers, where each number i means the value of variable i is flipped. The order matters.
Note that all the variables are initially 0.
Each ID number only appears once in the prefix Boolean expression.
The depth of the tree is no more than 30.
Output
For each testcase, you should output M lines, each showing the output of the prefix Boolean expression after a variable is flipped.
0 for false, 1 for true.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
The problem will ask you to create a binary search tree, and there will be 4 kinds of commands to complete
1. P : please print out the binary search tree in in-order form in th line ,if the tree is empty, please print "NULL"(There is an whitespace between each number, also after the last number,no space after NULL)
2. GetMax : print out the maximum height of the binary search tree( There need no space after output number)
3. SumLevel (input) : print out the sum value of the nodes at the input level in the line, if the input level is bigger than the maximum height, please print out "0". ( There need no space after output number)
4. AverLevel (input) : print out the average value of the nodes at the input level in the line, please show only 3 digits after decimal point of the average numbers of level, if the input level is bigger than the maximum height, please print out "0". (You can simply use %.3f to display in such way) ( There need no space after output number)
the root level will represent as 1( for SumLevel & AverLevel, if the input level is 0, please print "0")
Input
The first line contains an integer N , which indicates the number of nodes of the binary search tree.
The second line is the data for create binary search tree
The third line contains an integer M , which indicates the number of commands.
Following line will be the input instruction
If there is same input value, please ignore the second one.
Output
There need to print newline in the end.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Mr. Kuo is an adventurer. One day, he finds a secret room in a cave.
There is some hint to the password of this room.
Mr. Kuo is given an integer N and a string S = s1s2...sN consisting of L and R.
At first, Mr. Kuo has a string A = "0".
For each i = 1, 2, ..., N :
- If si is L, insert i to the left of the "number" i - 1 in A.
- If si is R, insert i to the right of the "number" i - 1 in A.
The final contents of A is the password. Please help Mr. Kuo find the password.
For example, N = 3 and S = "LRL", then:
- s1 = L, insert 1 to the left of 0, A = "10"
- s2 = R, insert 2 to the right of 1, A = "120"
- s3 = L, insert 3 to the left of 2, A = "1320"
Input
The first line contains one integer T — the number of test cases. Description of the test cases follows.
The first line of each test cases contains an integer N.
The second linef of each test cases contains a string S consisting of L and R of length N.
For each test:
- T ≤ 100, N ≤ 10
- T ≤ 100, N ≤ 100
- T ≤ 100, N ≤ 500
- T ≤ 100, N ≤ 1000
- T ≤ 100, N ≤ 10000
- T ≤ 10, N ≤ 100000
Output
For each test case print a string A — the final contents of the password, seperated by spaces.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Mr. Kuo is an adventurer. One day, he finds a maze.
The maze has N rooms. There may be a tunnel between rooms.
There is exactly one simple path between any two different rooms. That is, the maze is a "tree".
Mr. Kuo is curious about the length of the longest path in this maze.
Take the sample test as the example:
- The longest path of the 1st test case is (2 → 1 → 3), whose length is 2.
- The longest path of the 2nd test case is (1 → 2 → 3), whose length is 2.
- The longest path of the 3rd test case is (5 → 7 → 3 → 1 → 4 → 2), whose length is 5.
[Hint]
Instead of performing DFS twice, there is a solution to find the diameter in one DFS. For any tree whose root is \(i\), if we have two properties of its subtrees / children (\(j\)): the longest distance of the (sub-)tree (denoted by \(d\)) and the longest distance to any leaf (denoted by \(p\)). Then we have \(p_i=\displaystyle\max_{\forall j\text{ is a child of }i}(p_j+1)\), and \(d_i\) is either in its subtrees: \(\displaystyle\max_{\forall j\text{ is a child of }i}d_j\) or passing through the root: \(p_i+\displaystyle\max_{\forall j'\text{ is a child of }i}(p_{j'}+1)\), where \(j'\) mustn't be the same as we took in \(p_i\).
Input
The first line contains one integer T — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer N — the number of rooms in the maze.
Each of the next N - 1 line contains two integers ui and vi — there is a tunnel between ui and vi.
For each test:
- T ≤ 10, N ≤ 10
- T ≤ 10, N ≤ 100
- T ≤ 10, N ≤ 500
- T ≤ 10, N ≤ 1000
- T ≤ 10, N ≤ 10000
- T ≤ 30, N ≤ 20000
Output
For each test case print an integer — the length of the longest path in the maze.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Given a prefix expression, please evaluate its value.
The grammar of prefix expression could be defined as: EXPR := OP EXPR EXPR | CONSTANT, where OP is one of +, -, *, /, %. For division and modolus operations, we adapt C behavior, i.e., rounding to zero.
It's guaranteed that in each step of the calculation the value is always between [-263, 263).
Note
You can use atoi()
to convert a string to integer.
Input
There's only one line containing the prefix expression. There are spaces between each operands and operators. Each number in the expression is no less than -231 and less than 231.
Output
Please print out the value of the expression. Remember to put a newline character at the end of line.