ANALYSER - Program Analyser

no tags 


A Program which has the following format:

<Program>::=<sentence><line break>{<sentence><line break>}
<body>::=<addition> | <output> | <goto> | <condition> | <end>
<digit>::= 0|1|2|3|4|5|6|7|8|9
<line break>::=(ASCII 10)
<space>::=(ASCII 32)

The program runs following the following rules:

  • Program starts from the sentence whose level is minimum, and executed by the level from low to high except that the sentence is<goto>or<condition>.
  • All variables are initialized to 0.
  • <Addition>means<variable>+=<integer>in C++.
  • <output>means write the value of<variable>to the output file(we aren't interesting about the real output file.)
  • <condition>means if and only if the value of the <variable> equals to <integer>, <goto> will be executed, otherwise the next sentence executed is as usual.
  • After<goto>, the next sentence executed is the sentence with level which equals to the level in<goto>.
  • Program terminates by itself when <end> is executed.
  • The numbers during the program is running will fit in a signed 32-bit integer.
  • The number of sentences in the program is not more than 100.
  • The length of each line in the input file is not more than 20.
  • The input is correct.
  • The sentence with the maximum level is always <end>.
  • Any level is not more than 3000.

Input terminate by EOF.


Output the number of sentences executed.If the program can not terminate by itself,output -1.


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




You may try problem ANALYS_T first. It's the same problem with this one and its time limit is not so strict.


The time limit of this problem has been changed from 0.4/0.5 second to 1 second per test on Jun.5, 2008. All the solutions have been rejudged.

hide comments
Jacob Plachta: 2014-11-16 07:11:53

It seems that there's some issue with the input (in particular, with the format of a "goto" line). However, reading entire tokens instead of parsing each line character-by-character worked out...

:D: 2010-08-05 15:31:05

Are levels guaranteed to be distinct?

AUTO RE: I guess yes.

Last edit: 2012-11-26 20:08:47

Added by:Fudan University Problem Setters
Time limit:0.100s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO
Resource:Chinese National Olympiad in Informatics 2000,Day 1; translated by Blue Mary