| # | Problem | Pass Rate (passed user / total user) |
|---|---|---|
| 13128 | Kuo Wants To Parse |
|
| 13141 | KuoYangてぇてぇ — Birthday Gift |
|
Description
Kuo-chan wants to build a syntax tree for infix algebraic expressions with number, addition, subtraction, and parentheses.
To parse the infix expressions, he can use the grammar below.
EXPR = FACTOR | EXPR OP FACTOR
FACTOR = ID | (EXPR)
EXPR is the expression. ID is the number. OP is either + (addition) or - (subtraction).
Please help Kuo-chan to build the syntax tree.
You can use the following default code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h> //for isdigit()
typedef struct _Node {
int value;
char OP;
struct _Node *left;
struct _Node *right;
} Node;
char expr[120];
int pos;
Node *head;
Node* Factor();
Node* Expr();
void PrefixPrint(Node* node);
Node* CreateNode(int value , char ch) {
Node *newNode;
newNode = (Node*) malloc(sizeof(Node));
if(ch == 'N') {
newNode->value = value;
newNode->OP = 'N';
}
else {
newNode->value = -1;
newNode->OP = ch;
}
return newNode;
}
int main() {
while((scanf("%s" , expr)) != EOF) {
pos = strlen(expr) - 1;
head = Expr();
PrefixPrint(head);
printf("\n");
head = NULL;
}
return 0;
}
Input
There're multiple testcases.
Every number is positive and smaller than 109.
All parentheses are matched.
The length of every input is at most 100.
Different testcases have different constraints.
- All number is smaller than 10. No parentheses.
- All number is smaller than 10. No nested parentheses such as (1+(2)), but (1)+(2) is permitted.
- All number is smaller than 10.
- No parentheses.
- No nested parentheses.
- No extra constraints.
Output
The output contains N prefix expressions without parentheses, which are preorders of syntax trees.
Please ouput a whitespace after every number, addition, and subtraction. Read the sample output for more detail.
Remember to add new line in the end.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Kuo-chan gives Yang-chan an empty sequence A as his birthday gift.
Yang-chan is allowed to perform the following operation:
- PUSH x — push x to the end of A
- POP — remove the median value of A if A is not empty
- PROGRAMMING TANOSHI k — for 1 ≤ i ≤ |A|, assign ⌈ A[ i ] / k ⌉ (ceil) to A[ i ]
- IP2 SUGOI — for 1 ≤ i ≤ |A|, assign ⌊ A[ i ]0.5 ⌋ (floor) to A[ i ]
- the median value of A is A[ (|A|+1)/2 ]
- ⌈ x ⌉ is the least integer greater than or equal to x
- ⌊ x ⌋ is the greatest integer less than or equal to x
For example, initially A = []
PUSH 4 ► A = [4]
PUSH 3 ► A = [4, 3]
PUSH 5 ► A = [4, 3, 5]
PUSH 11 ► A = [4, 3, 5, 11]
POP ► A = [4, 5, 11]
POP ► A = [4, 11]
PROGRAMMING TANOSHI 3 ► A = [2, 4]
IP2 SUGOI ► A = [1, 2]
Kuo-chan is curious about what A is after Yang-chan performs some operations to it.
Help him find it out!
Input
The input contains Q lines.
Initially, A is empty.
Each of the Q lines describes the operations Yang-chan performs. Each line is one of four types:
- PUSH x (1 ≤ x ≤ 109 )
- POP
- PROGRAMMING TANOSHI k ( 1 ≤ k ≤ 9 )
- IP2 SUGOI
Different testcases have different constraints.
- Q ≤ 2*103, operations: { PUSH, POP }
- Q ≤ 2*103, operations: { PUSH, POP, PROGRAMMING TANOSHI }
- Q ≤ 2*103, operations: { PUSH, POP, PROGRAMMING TANOSHI, IP2 SUGOI }
- Q ≤ 3*105, operations: { PUSH, POP }
- Q ≤ 3*105, operations: { PUSH, POP, PROGRAMMING TANOSHI }
- Q ≤ 3*105, operations: { PUSH, POP, IP2 SUGOI }
- Q ≤ 3*105, operations: { PUSH, POP, PROGRAMMING TANOSHI, IP2 SUGOI }
- Q ≤ 3*105, operations: { PUSH, POP, PROGRAMMING TANOSHI, IP2 SUGOI }
Note: You can include <math.h> to use sqrt(x). sqrt(x) returns the square root of x. Just be careful with roundoff error.
You can also use ceil(x), floor(x) included in <math.h>.
Hint: To speed up the code, you can think about how many operations will an element be performed at most.
Update: 1. Make use of the variable all_operation to store {PROGRAMMING TANOSHI, IP2 SUGOI} operations.
2. You can think that if a number becomes 1 you won't need to do any operation on it anymore.
Output
You should print one line containing the elements of A after the operations. For 1 ≤ i ≤ |A|, the i-th number should be A[ i ].