ZigZag
InterviewTime limit2sMemory limit64 MB
Given K words and N letters, output for each letter the word starting with it that has been used fewest times, breaking ties alphabetically.
- Level
Medium4 of 10
- Topics
- Sorting, Hash map, Implementation, Greedy
- Solved
- No attempts yet
Problem
Zig and Zag play a word game. Zig says one lowercase letter, and Zag answers with a word that starts with that letter. The word must come from a fixed word list, and among the words on the list that start with that letter, Zag says the one he has said the fewest times so far. If several words tie, Zag says the one that comes first in alphabetical order. For every letter Zig says, a word is always available.
You are given a list of distinct words and the letters Zig says. Write a program that prints the words Zag says, in order.
Input
The first line contains () and ().
Each of the next lines contains one word, a string of lowercase English letters of length at most .
Each of the next lines contains one lowercase English letter that Zig says.
Output
Print lines. Line contains the word Zag says in answer to the -th letter.