spoj#ANALYSER. Program Analyser
Program Analyser
Input
A Program which has the following format:
<Program>::=<sentence><line break>{<sentence><line break>} <setence>::=<level><space><body> <body>::=<addition> | <output> | <goto> | <condition> | <end> <addition>::=<variable>+<integer> <output>::=<variable>? <goto>::=GO<space><level> <condition>::=IF<space><variable>=<integer><space><goto> <end>::=END <variable>::=<character> <level>::=<integer> <integer>::=<digit>{<digit>} <character>::=A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z <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
Output the number of sentences executed.If the program can not terminate by itself,output -1.
Example
Input: 10 A+1 20 IF A=5 GO 60 60 END 30 A+2 40 A? 50 GO 20</p>Output: 11
Hint: 10->20->30->40->50->20->30->40->50->20->60
Note
You may try problem ANALYS_T first. It's the same problem with this one and its time limit is not so strict, but tests are easier than this problem.
Log
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.
Due to compiler changes, the time limit has been shorten to 0.1 second per test.