#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 ii (the variable ii must not have the same name as any existing variable that has not been destroyed) and initializing it to xx. Then compare ii with yy: if ii is less than or equal to yy, enter the loop; otherwise, do not enter. After each loop, ii is updated to i+1i+1. Once ii becomes greater than yy, the loop terminates.

xx and yy can be positive integers (the relation between xx and yy is not fixed) or the variable nn. The variable nn represents the problem size; in time complexity analysis, nn must be kept as a variable and not treated as a constant. It is much larger than 100100.

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 O\operatorname O to denote the concept of Θ\Theta in the usual sense when describing complexity.

Input Format

The first line of input contains a positive integer tt, meaning there are tt (t10t \le 10) 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 LL and a string. LL 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 nwn^w, where ww is a positive integer less than 100100. The input guarantees that the claimed complexity is only one of O(1) or O(n^w).

The next LL 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 ii is a lowercase letter (guaranteed not to be nn) representing the newly created variable name, and xx and yy may be positive integers or nn. If they are positive integers, they are guaranteed to be less than 100100.

If a line starts with E, it indicates the end of a loop body.

Output Format

Output exactly tt 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: ii goes from 11 to 11, which is constant complexity.
  • The second program: xx goes from 11 to nn, which is linear in nn.
  • The third program: there is an F that starts a loop without a matching E, which is a syntax error.
  • The fourth program: two nested loops, so the complexity is n2n^2.
  • The fifth program: two separate single loops, so the complexity is linear in nn.
  • The sixth program: the first loop is normal, but the second loop terminates immediately (because nn is much larger than 100100, and 100100 is larger than 44).
  • The seventh program: the first loop cannot be entered, so it is constant complexity.
  • The eighth program: the variable xx in the second nested loop duplicates the variable from the first loop, which triggers syntax error ②, so output ERR.

[Constraints and Notes]

  • For 30%30\% of the testdata: no syntax errors. It is guaranteed that the first L/2L/2 lines are F-starting statements, and lines L/2+1L/2+1 to LL are E-starting statements. L10L \le 10. If xx and yy are both integers, then x<yx < y, and only yy may be nn.
  • For 50%50\% of the testdata: no syntax errors. L100L \le 100. If xx and yy are both integers, then x<yx < y, and only yy may be nn.
  • For 70%70\% of the testdata: no syntax errors. L100L \le 100.
  • For 100%100\% of the testdata: L100L \le 100.

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