You are given a sentence consisting of several space-separated words and a list of pairs of synonyms. Your task is to find out all possible sentences that can be formed by replacing any word in the sentence with any of its synonyms.
Note :
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.
Output Format :
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.
Note :
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
1
2 5
bright pleasant happy bright
today is a pleasant day
today is a bright day
today is a happy day
today is a pleasant day
In the first test case, we can see that “bright” is a synonym of “pleasant”. We can also see that “happy” is also a synonym of “bright”. This means that “bright”, “pleasant” and “happy” are all synonyms. Hence we can form three different sentences using these words one by one.
Try to find all groups of synonyms.
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.
Steps:
void findSynonyms(string word, set<string>& wordList, unordered_map<string, set<string>>& groups)
void useSynonym(vector<string> currentSentence, unordered_map<string, set<string>>& groups,
vector<vector<string>>& allSentences, int idx)
O(N^M), where N is the number of pairs of synonyms, and M is the number of words in the sentence.
In the worst case, all words in the list of synonyms can be synonyms of a single word. So, that word can have N synonyms. If all the words in the sentence are synonyms of each other, we can replace each word with its N synonyms, and since there are M words in the sentence, the time complexity becomes O(N^M).
O(N^M), where N is the number of pairs of synonyms, and M is the number of words in the sentence.
We need extra space to maintain hashmaps to store the synonyms for every word of the sentence. Since there can be N different synonyms for each word, we need extra O(N^M) space.