#P5756. [NOI2000] 程序分析器

[NOI2000] 程序分析器

Description

The Backus–Naur Form (BNF) of the Tiny Basm language (abbreviated as TB) is:

Examples of incorrect statements (no incorrect statements will appear in the input file):

  • 10 A+1.5 (does not match the definition of an addition statement, because the added value is not an integer)
  • 20 A ? (does not match the definition of an output statement, because there is an extra space)
  • 30 IF A=B GO 10 (does not match the definition of a conditional statement; it should not be variable == variable)

\\

Execution of a TB program:

  • The program starts executing from the statement with the smallest line number. Before encountering any conditional statement, it executes statements in increasing order of line number.
  • All variables are automatically initialized to 00 before execution.
  • An addition statement adds the integer in the statement to the value of the variable, and stores the result back into that variable.
  • An output statement displays the value of the variable in the statement on the monitor.
  • When executing a conditional statement, the following jump statement is executed if and only if the variable in the statement is equal to the integer value immediately after the equals sign. All integer values in this statement are at most 44 digits.
  • After a jump statement is executed, the program jumps to the statement with the line number specified after GO\tt GO.
  • When the program executes an end statement, the entire program terminates.
  • Assume the system can handle integers of any size without overflow.

Please write a program that, for a given TB program PP, computes the number of statements executed by the program (for a conditional statement, regardless of whether the jump succeeds, it is counted as executing exactly one statement).

Input Format

  • The input is a TB program PP, with no more than 100100 statements (lines).
  • The length of each statement in PP does not exceed 2020 characters.
  • For every jump statement in PP, the line number after GO\tt GO definitely has a corresponding statement.
  • PP may contain multiple end statements with different line numbers.
  • The statement with the largest line number in PP is definitely an end statement.
  • All line numbers in PP are no greater than 30003000.
  • The input file does not necessarily list PP in increasing order of line numbers.

Output Format

  • There is exactly one line:

If the program can terminate normally, output the number of statements executed;

if the program cannot terminate normally, output 1-1.

10 A+1
20 IF A=5 GO 60
60 END
30 A+2
40 A?
50 GO 20

11

Hint

Sample Explanation

The executed line numbers in order are 102030405020304050206010→20→30→40→50→20→30→40→50→20→60.

A total of 1111 statements are executed.

Translated by ChatGPT 5