
1. A word may or may not have synonyms.
2. There can be multiple synonyms for a single word.
3. All words only contain lowercase English letters.
The first line contains an integer T, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains two space-separated integers, N and M, denoting the number of pairs of synonyms in the list and the number of words in the sentence.
The second line contains 2*N space-separated words. For every, i from 1 to N, (2*i-1)-th and (2*i)-th words are synonyms of each other.
The third line contains M space-separated words which form the sentence.
For each test case, print in separate lines all sentences that can be formed by replacing one or more words with their synonyms.
Please note that all the sentences must be printed in lexicographically sorted order.
The output of every test case will be printed in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N,M <= 8
1 <= |word| <= 10
Where |word| denotes the length of any word that appears in the sentence.
Time Limit: 1sec
The approach is to use hashing and recursion to find out different groups of words so that every word of a group is a synonym of every other word in that particular group. Now, replace every possible word with their respective synonyms one by one using recursion and when all the words for this traversal have been replaced, push this sentence into the vector and continue traversing to find all other possible sentences.
Sorted Doubly Linked List to Balanced BST
Longest Substring with K-Repeating Characters
Expression Add Operators
Gray Code Transformation
Count of Subsequences with Given Sum