Colliding Encoding
InterviewTime limit20sMemory limit1024 MB
Given a digit for each letter A to Z and a list of words, determine whether two different words encode to the same digit string.
Problem
Alan just had his first cryptography class in school today. He decided to apply what he learned and came up with his own cipher. He maps each English letter from A to Z to a decimal digit from to . He then encodes each word as a string of decimal digits by replacing each letter with its mapped digit.
Alan did not notice that there are letters and only decimal digits. As a result, collisions are possible, meaning two different words can have the same encoding.
Given a list of words and the mapping Alan uses, determine whether any collisions exist among the words on the list.
Input
The first line of the input contains the number of test cases, . Then test cases follow.
The first line of each test case contains 26 decimal digits (integers from to , inclusive) . Letter is mapped to digit .
The second line of each test case contains , the number of words.
Each of the next lines contains one word .
Output
For each test case, output one line containing Case #x: y, where is the test case number starting from 1. Output YES if at least one pair of different words on the list has the same encoding. Otherwise, output NO.
Constraints
- .
- for all letters .
- the length of for all .
- Each character of is an uppercase English letter from
AtoZ. - for all .
Hint
In Sample Case #1, the mapping for A is , for B is , for C is , for D is , and for E is . With this mapping, ABC is encoded as , BC is encoded as , BCD as , and CDE as . Since all of these encodings are distinct, there are no collisions.
In Sample Case #2, the mapping for C is , for D is , for E is , for F is , and for G is . With this mapping, CDE is encoded as , DEF as , and EFG as . Since the encodings for DEF and EFG are the same, there is a collision.