Puzzlestan
Time limit1sMemory limit128 MB
Given N groups of M lettered items and statements about which items share or do not share an owner, reconstruct the full assignment of items to guests.
- Level
Medium7 of 10
- Topics
- Union-find, Backtracking, Brute force, Implementation
- Solved
- No attempts yet
Problem
There are kinds of items and guests. Each guest owns exactly one item of every kind (for example, each guest checks in one overcoat, one hat, and so on). Hence there are items in total, and each item has a distinct single-letter name (letters are case-sensitive).
The items of kind () are written as a string of length , called the -th group. The -th character of this string is the -th item of group .
You are given several statements telling, for two items, whether they belong to the same guest or to different guests. Each statement is one of:
- The two items belong to the same guest.
- The two items belong to different guests.
Using these statements, determine which guest owns each item. The statements always determine the assignment uniquely.
Input
The first line contains the number of test cases (at most ).
The first line of each test case contains two positive integers and , where () is the number of item kinds and () is the number of guests. Each of the next lines contains one string of length representing a group (the distinct items of one kind).
After that come several statement lines. Each statement has the form i j X k r, meaning that the -th item of group and the -th item of group belong to the same guest if is R, or to different guests if is N. The last line of each test case is the dummy statement 0 0 R 0 0.
Output
For each test case, print lines. Line () describes the guest identified by the -th item of the first group: print that guest's item from group 1, then from group 2, ..., then from group , as letters with no separator. Thus the first character of each line matches the first group in order.
Separate the output of consecutive test cases by exactly one blank line.