cswork / OAOpen practice workspace

HarveyOA #2

Spreadsheet Formula Evaluator

Estimated MediumJudge-ready

Topics (AI-labelled)IteratorsTopo Sort

Implement SET label expression and GET label. An expression consists only of non-negative integers and cell labels joined by plus signs. A SET that would create a dependency cycle is rejected and the old value is kept; GET returns ERROR for a cell that is unset or whose reference chain cannot be evaluated.

Input

Each line contains one command; there are at most 1000 commands and at most 300 distinct labels. A label is an uppercase letter followed by digits; an addition expression has at most 50 terms, literals are ≤10^9, and every evaluable result is guaranteed to fit in a signed 64-bit integer. Referencing an unset cell does not form a cycle; GET returns ERROR in that case.

Output

For each command, output OK, ERROR or an integer.

Example 1

Input:

SET A1 7
GET A1

Output:

OK
7

Example 2

Input:

SET A1 B1
GET A1
SET B1 4
GET A1

Output:

OK
ERROR
OK
4

Example 3

Input:

SET A1 B1
SET B1 A1
GET A1

Output:

OK
ERROR
ERROR

Editorial

Loading solution…