#P3952. [NOIP 2017 提高组] 时间复杂度
[NOIP 2017 提高组] 时间复杂度
Description
Xiao Ming is learning a new programming language, A++. After just learning loop statements, he excitedly wrote many programs and gave the time complexity he calculated for each. His programming teacher does not want to check them one by one, so here is your chance! Please write a program to judge whether the time complexity Xiao Ming gave for each program is correct.
The loop structure in A++ is as follows:
F i x y
loop body
E
Here, F i x y means creating a new variable (the variable must not have the same name as any existing variable that has not been destroyed) and initializing it to . Then compare with : if is less than or equal to , enter the loop; otherwise, do not enter. After each loop, is updated to . Once becomes greater than , the loop terminates.
and can be positive integers (the relation between and is not fixed) or the variable . The variable represents the problem size; in time complexity analysis, must be kept as a variable and not treated as a constant. It is much larger than .
E indicates the end of the loop body. When the loop body ends, the variables created by this loop are destroyed.
Note: For convenience, this problem uses the uppercase letter to denote the concept of in the usual sense when describing complexity.
Input Format
The first line of input contains a positive integer , meaning there are () programs whose time complexity needs to be checked. For each program, we only need to extract the F i x y and E lines to compute the time complexity. Note: Loop structures can be nested.
For each program, the first line contains a positive integer and a string. is the number of lines in the program, and the string denotes the program’s claimed complexity. O(1) means constant complexity, and O(n^w) means the complexity is , where is a positive integer less than . The input guarantees that the claimed complexity is only one of O(1) or O(n^w).
The next lines are either F i x y or E, representing the loop structure in the program. If a line starts with F, it indicates entering a loop, followed by three space-separated tokens i x y, where is a lowercase letter (guaranteed not to be ) representing the newly created variable name, and and may be positive integers or . If they are positive integers, they are guaranteed to be less than .
If a line starts with E, it indicates the end of a loop body.
Output Format
Output exactly lines, each corresponding to one program in the input. For each program, output Yes, No, or ERR. Output Yes if the actual complexity matches the given complexity; output No if it does not match; output ERR if there is a syntax error. The only syntax errors are: ① mismatched F and E; ② creating a variable with the same name as an already existing but not yet destroyed variable.
Note: Even if a syntax error occurs inside a loop body that will never execute, it is still a compile-time error, and you must output ERR.
8
2 O(1)
F i 1 1
E
2 O(n^1)
F x 1 n
E
1 O(1)
F x 1 n
4 O(n^2)
F x 5 n
F y 10 n
E
E
4 O(n^2)
F x 9 n
E
F y 2 n
E
4 O(n^1)
F x 9 n
F y n 4
E
E
4 O(1)
F y n 4
F x 9 n
E
E
4 O(n^2)
F x 1 n
F x 1 10
E
E
Yes
Yes
ERR
Yes
No
Yes
Yes
ERR
Hint
[Explanation of Sample 1]
- The first program: goes from to , which is constant complexity.
- The second program: goes from to , which is linear in .
- The third program: there is an
Fthat starts a loop without a matchingE, which is a syntax error. - The fourth program: two nested loops, so the complexity is .
- The fifth program: two separate single loops, so the complexity is linear in .
- The sixth program: the first loop is normal, but the second loop terminates immediately (because is much larger than , and is larger than ).
- The seventh program: the first loop cannot be entered, so it is constant complexity.
- The eighth program: the variable in the second nested loop duplicates the variable from the first loop, which triggers syntax error ②, so output
ERR.
[Constraints and Notes]
- For of the testdata: no syntax errors. It is guaranteed that the first lines are
F-starting statements, and lines to areE-starting statements. . If and are both integers, then , and only may be . - For of the testdata: no syntax errors. . If and are both integers, then , and only may be .
- For of the testdata: no syntax errors. .
- For of the testdata: .
If you need to hack, please private message @zhouyonglong or start a discussion, and provide testdata and an AC submission that this problem can be hacked by.
Translated by ChatGPT 5
京公网安备 11011102002149号