P2830 Write a Program
Background
zrz is writing a program. He asks you to check whether there are any problems with his program.
Description
There is a program with several lines, and each line contains exactly one command. The possible commands are as follows:
- `int a[N]` Declare an array. The keyword is always `int`, never something else like `long long`. `a` denotes the name of an array (not necessarily `a`; it could be another letter or multiple letters, with length no more than $10$). After that is a pair of brackets containing either a number or a variable, indicating the array size (indices range from $0$ to $N-1$, $N\le100$). After the array is declared, all its elements are $0$.
- `a[i] h` Assign `h` to `a[i]` (i.e., `a[i]=h`). Here $h$ can be either a number or a variable, and $i$ can be either a number or a variable.
- `cout h` Output $h$, where $h$ must be a variable.
Input Format
Several lines: one command per line.
Output Format
For each output command (i.e., `cout`), print one line.
If an out-of-bounds array index is found on any line (note that only this type of error can occur; there will not be other issues such as redefinition), stop immediately regardless of how many commands remain below, ignore all remaining commands, and output `-1`.
Explanation/Hint
The number of lines does not exceed $10$. Variables may be nested, such as `a[a[b[0]]]`, etc. Uppercase letters may also appear. All numbers that appear do not exceed $10^9$, are not negative, and are not decimals.
Translated by ChatGPT 5