#P2830. 写程序

写程序

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 1010). After that is a pair of brackets containing either a number or a variable, indicating the array size (indices range from 00 to N1N-1, N100N\le100). After the array is declared, all its elements are 00.
  • a[i] h Assign h to a[i] (i.e., a[i]=h). Here hh can be either a number or a variable, and ii can be either a number or a variable.
  • cout h Output hh, where hh 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.

int a[10]
a[a[0]] 2
cout a[0]
2
int a[10]
a[0] 10
cout a[0]
a[a[0]] 1
cout a[0]
10
-1

Hint

The number of lines does not exceed 1010. Variables may be nested, such as a[a[b[0]]], etc. Uppercase letters may also appear. All numbers that appear do not exceed 10910^9, are not negative, and are not decimals.

Translated by ChatGPT 5