#C14495. Anagram Pair Finder
Anagram Pair Finder
Anagram Pair Finder
You are given two lists of words. The first list contains N words and the second list contains M candidate words. Two words are considered anagrams if and only if their letters, when sorted in lexicographical order, are identical. In other words, if we let \( sorted(s) \) denote the string formed by sorting the characters of string s in increasing order, then two words a and b are anagrams if \( sorted(a)=sorted(b) \).
Your task is to find all pairs \((word, candidate)\) such that word
comes from the first list and candidate
is from the second list and they are anagrams of each other. The order of pairs should correspond to the order of the words in the first list.
If a word has a corresponding candidate anagram (even if there are duplicate entries), output the pair.
inputFormat
The input is read from standard input and has the following format:
- The first line contains an integer N, the number of words in the first list.
- The second line contains N space-separated words.
- The third line contains an integer M, the number of candidate words.
- The fourth line contains M space-separated words.
outputFormat
For each anagram pair found, output a line with the two words separated by a single space. The output should be printed to standard output. If no anagram pairs are found, output nothing.
## sample3
listen binary tea
3
silent brainy eat
listen silent
binary brainy
tea eat
</p>