Given switch-operation patterns and resulting bulb states over Q steps, determine which of N switches controls each bulb, or mark it unknown. Standard whiteboard task? No.
Hard8Bit manipulationMathBrute forceImplementationNo attempts yetTime limit8sMemory limit512 MBA building has M light bulbs, and N switches control them. Every bulb is wired to exactly one switch, and one switch may control several bulbs. Operating a switch flips the state of every bulb that switch controls.
The table that recorded which switch controls which bulb is lost. You want to rebuild it with the following procedure.
Operating switches and reading bulbs leaves both the switches and the bulbs in their new states, and the next operation continues from there.
Rebuild the correspondence between the switches and the bulbs from the switches you operated and the bulb states you read.
The input holds several datasets. There are at most 50 datasets and the whole input is at most 10MB. Each dataset has this format.
N M Q
S1 B1
:
:
SQ BQ
The first line holds three integers N, M and Q: the number of switches, the number of bulbs and the number of operations. (1≤N≤36, 1≤M≤1000, 0≤Q≤1000)
Each of the next Q lines holds two strings Si and Bi of lengths N and M, separated by a space. The j-th character of Si is 0 or 1: 0 means the j-th switch was not operated, 1 means it was. The j-th character of Bi is 0 or 1: 0 means the j-th bulb is off, 1 means it is on.
At least one correspondence between the switches and the bulbs is consistent with the given information.
A line holding three zeros marks the end of the input.
For each dataset, print the correspondence on one line as M base-36 digits. In the base-36 system of this problem, the values 0 to 9 are written as the characters '0' to '9' and the values 10 to 35 as the characters 'A' to 'Z'. Switches are numbered starting from 0.
The i-th character is the number of the switch that controls the i-th bulb. If the switch controlling the i-th bulb cannot be pinned down to one switch, print '?' instead of a number.