#P4432. [COCI2017-2018#2] ​​ZigZag

[COCI2017-2018#2] ​​ZigZag

题目描述

Zig and Zag are playing a word game. Zig says one letter, and Zag says a word that starts with that letter. However, the word needs to be from the allowed word list and such that Zag already said it the least amount of times. If the word choice is ambiguous, then Zag will choose the one that is lexicographically smaller (sooner in the alphabet). For each Zig’s letter, it will be possible to choose a word.

Let there be a list consisting of exactly K distinct words and an array of N letters that Zig has given. Write a program that will, based on the input, output an array of N words that Zag said during the game.

输入格式

The first line of input contains positive integers K (1 ≤ K ≤ 100 000) and N (1 ≤ N ≤ 100 000) from the task.

Each of the following K lines contains a single word consisting of lowercase letters of the English alphabet not longer than 21 characters.

Each of the following N lines contains a single lowercase letter of the English alphabet.

输出格式

You must output N lines, each containing a single word from the task.

题目大意

Zig和Zag正在玩文字游戏。Zig说了一个字母,而Zag说了一个以该字母开头的单词。但是这个词需要出现在给出的单词列表中,并且被是相同首字母中使用的次数最少的单词。如果单词的选择不明确(即相同首字母中使用的次数最少的单词不止一个),那么Zag会选择字典序较小的字母。输入保证对于每个Zig的字母,都有可以选择的单词。

假设有一个由K个不同的单词组成的列表和一个Zig给出的N个字母组成的列表。编写一个程序,根据输入,输出Zag在游戏过程中说出的N个单词。

输入输出格式

输入格式: 第一行输入包含来自的正整数K(1≤K≤100 000)和N(1≤N≤100 000)。

以下K行是K个单词,由小写英文字母组成,不超过21个字母。

以下N行是Zig说的N个小写英文字母。

输出格式: N行,分别对应N个Zig的询问。

感谢@K_Vin 提供的翻译

4 5
zagreb
split
zadar
sisak
z
s
s
z
z

zadar
sisak
split
zagreb
zadar

5 3
london
rim
pariz
moskva
sarajevo
p
r
p

pariz
rim
pariz

1 3
zagreb
z
z
z

zagreb
zagreb
zagreb

提示

In test cases worth 60% of total points, it will hold that N and K are smaller than 500.