Unknown Switches
Time limit8sMemory limit512 MB
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.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Math, Brute force, Implementation
- Solved
- No attempts yet
Problem
A building has light bulbs, and 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.
- At the start every switch is off and every bulb is off.
- You operate the switches given by .
- You check the bulbs and read the states .
- You operate the switches given by .
- You check the bulbs and read the states .
- You repeat this up to and .
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.
Input
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 , and : the number of switches, the number of bulbs and the number of operations. (, , )
Each of the next lines holds two strings and of lengths and , separated by a space. The -th character of is 0 or 1: 0 means the -th switch was not operated, 1 means it was. The -th character of is 0 or 1: 0 means the -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.
Output
For each dataset, print the correspondence on one line as 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 -th character is the number of the switch that controls the -th bulb. If the switch controlling the -th bulb cannot be pinned down to one switch, print '?' instead of a number.