P2229 [HNOI2002] Desert Treasure Hunt
Description
It is said that there are ruins of an ancient city buried with a large amount of treasure in the boundless desert. One after another, people who heard the news went to look for treasure, but none returned.
The explorer Jack is a rising star among the adventurers. Although he joined the team not long ago, he and his teammates have repeatedly escaped danger thanks to his extraordinary wisdom. “Knowing there are tigers on the mountain, we head for the mountain anyway.” Guided by the creed of Jack’s expedition, Jack and his teammates set off on a desert treasure hunt.
“I found the desert ruins.” With a teammate’s shout, Jack vaguely saw some broken walls of an ancient city and a passage. “This must be it.” Jack led everyone into the passage.
However, misfortune struck. As Jack and the others entered the passage, the entrance slammed shut with a bang. Jack and his teammates reached the end of the passage, and in front of them was a huge iron gate. Strangely, the gate used an extremely advanced electronic lock with a keyboard and display, and the unlocking method was written clearly beside the gate: “Those who wish to obtain my treasure, the journey here must have been arduous! But it will soon be over; this iron door will be your tomb. Unless you can compute the result of the program shown on my screen and enter it through the keyboard, this door will open, and inside lies all my treasure.”
True to expectations, Jack opened his pack and took out something like a safe. “Never seen one? This is the latest laptop. Without it, no matter how much of a genius I am, I couldn’t open this door.” Facing everyone’s puzzled looks, Jack turned on the laptop.
“This treasure owner’s program is very strange. Although I haven’t seen it before, based on my guess, its structure should be as follows:
The program contains 6 types of statements: start, end, loop, continue, write, and statements of the form ?=\*.
start marks the beginning of the program and corresponds to a closing marker end.
loop is followed by a space and then an expression \*. The value N of expression \* indicates that the following block will loop N times; the corresponding loop closing marker is also end.
continue means the program jumps to the end statement corresponding to the current loop, and break means the program exits the current loop.
write is followed by a space and then an expression \*, indicating that the value of expression \* should be output, which is what must be entered into the password lock.
?=\* is an assignment statement, where ? is the variable name, and the computed value of \* is assigned to ?.
Fortunately, variable names are restricted to the 26 lowercase letters 'a'..'z'. Expressions are restricted to the four arithmetic operations (addition, subtraction, multiplication, division) and parentheses; operands can only be the 26 variables or integers (multi-digit integers are allowed). An expression will not exceed 80 characters.”
“Hope Jack can open the lock soon.” Everyone prayed silently.
The sum of the execution counts of all statements in the given program is less than 2000000.
Input Format
The input file is program.in, which contains the treasure owner’s program. Any two statements are separated by one or more spaces or newline characters.
Output Format
The output file is program.out, which is the content you must enter into the password lock. Output one result per line.
Explanation/Hint
Translated by ChatGPT 5