H: Modelling Problems

https://open.kattis.com/problems/modelling

There are no more than 10 unknown variable assignments [x=?]. Moreover, each ? can either be 0 or 1. So, we can iterate over all 10-bit integers (0, ..., 1023) to consider all possible assignment combinations for ? terms.

For each 10-bit integer i:

  1. Create a hash map that maps variable names (chars) to values (ints)

  2. Parse statements from the input string. This can just be done by checking all cases and handling each case separately:

    1. Assignment: map var name to a value (either a given value or some of two other vars)

    2. Assignment to an unknown: maintain a count c of the unknowns seen so far. Then, treat the unknown as the value (i >> c) & 1 and increment c by 1. This sets the unknown to the c-th bit of i.

    3. Conditional: check if the condition is true based on entries in the map, and parse the assignments/assertions within the conditional if so

    4. Assertions: check if the variables specified have the same value. If not, immediately finish the test case and output ASSERTIONS INVALID

If all assertions pass for all i, output ASSERTIONS ALWAYS HOLD

Note that variables have a default value of 0. In C++/Java, make sure to always check if a variable is in the map and use the value 0 otherwise. In Python, you can use a defaultdict to default variables not in the map to a value of 0.

Last updated