Charging Chaos (Small)
Time limit5sMemory limit512 MB
Find the fewest bit positions to flip so the outlet strings match the device strings after reordering.
- Level
Medium4 of 10
- Topics
- Brute force, Bit manipulation, Hash map
- Solved
- No attempts yet
Problem
Shota the farmer has just moved into his newly built farmhouse, and the outlets are not configured for his devices. Being a modern farmer, Shota owns several smartphones and laptops, and even a tablet for his favorite cow Wagyu. He owns devices in total.
The devices have different specifications and come from different companies, so each one needs a different electric flow to charge. Each outlet in the house also puts out one specific electric flow. An electric flow is a string of 0s and 1s of length .
Shota wants to charge all devices at the same time. His new house has exactly outlets. A master control panel with switches configures the flow from the outlets. Pressing switch flips the th bit of the electric flow of every outlet in the house. For example, suppose the outlets put out these flows:
Outlet 0: 10
Outlet 1: 01
Outlet 2: 11
Pressing the second switch reconfigures them to:
Outlet 0: 11
Outlet 1: 00
Outlet 2: 10
If Shota has a smartphone that needs the flow 11, a tablet that needs 10, and a laptop that needs 00, then pressing the second switch charges all three.
Misaki is hired to sort this out. She has measured the flow of every outlet in the house and found that no two outlets put out the same flow. Decide whether Shota can charge all of his devices at the same time, and if he can, find the smallest number of switches that has to be pressed. The switches are big and heavy, so Misaki presses only as many as she needs.
Devices and outlets can be paired in any order.
Input
The first line contains the number of test cases . test cases follow. Each test case takes three lines. The first line contains two integers and separated by a space. The second line contains strings of length , separated by spaces, giving the initial electric flow of the outlets. The third line contains strings of length , in the same format, giving the electric flow each device needs.
Limits
- No two outlets put out the same flow initially.
- No two devices need the same flow.
Output
For each test case, print one line in the form Case #x: y. Here is the test case number, starting at 1, and is the smallest number of switches that has to be pressed so Shota charges every device. If no set of switches works, print NOT POSSIBLE in place of , without the quotes. The spelling, the capital letters, and the single space have to match exactly.
Note
In the first test case of the first example, Misaki presses the second switch once. The flows become:
Outlet 0: 00
Outlet 1: 10
Outlet 2: 11
Shota then charges device 1 from outlet 0, device 2 from outlet 1, and device 0 from outlet 2. Fewer than one press cannot charge all three devices.