from heapq import heappop, heappush
import re
from typing import Counter, List
class Down:
def __init__(self, value):
self.value = value
def __lt__(self, other):
return self.value > other.value
def top_mentioned(k: int, keywords: List[str], reviews: List[str]) -> List[str]:
patt = re.compile(r'\b(:?{})\b'.format('|'.join(keywords)), flags=re.IGNORECASE)
counts = Counter(
word
for review in reviews
for word in set(match[0].lower() for match in patt.finditer(review))
)
queue = []
for word, count in counts.items():
heappush(queue, (count, Down(word)))
if len(queue) > k:
heappop(queue)
res = []
while len(queue) > 0:
res.append(heappop(queue)[1].value)
return res[::-1]
if __name__ == '__main__':
k = int(input())
keywords = [input() for _ in range(int(input()))]
reviews = [input() for _ in range(int(input()))]
res = top_mentioned(k, keywords, reviews)
print(' '.join(res))
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
public static List<String> topMentioned(int k, List<String> keywords, List<String> reviews) {
Pattern patt = Pattern.compile("\\b(:?" + String.join("|", keywords) + ")\\b", Pattern.CASE_INSENSITIVE);
HashMap<String, Integer> counts = new HashMap<>();
for (String review : reviews) {
Matcher m = patt.matcher(review);
HashSet<String> words = new HashSet<>();
while (m.find())
words.add(m.group(0).toLowerCase());
for (String word : words)
counts.merge(word, 1, Integer::sum);
}
PriorityQueue<Map.Entry<Integer, String>> queue = new PriorityQueue<>((a, b) -> {
if (a.getKey() != b.getKey())
return Integer.compare(a.getKey(), b.getKey());
return -a.getValue().compareTo(b.getValue());
});
for (Map.Entry<String, Integer> entry : counts.entrySet()) {
queue.offer(Map.entry(entry.getValue(), entry.getKey()));
if (queue.size() > k)
queue.poll();
}
ArrayList<String> res = new ArrayList<>();
while (!queue.isEmpty())
res.add(queue.poll().getValue());
Collections.reverse(res);
return res;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int k = Integer.parseInt(scanner.nextLine());
int keywordsLength = Integer.parseInt(scanner.nextLine());
List<String> keywords = new ArrayList<>();
for (int i = 0; i < keywordsLength; i++) {
keywords.add(scanner.nextLine());
}
int reviewsLength = Integer.parseInt(scanner.nextLine());
List<String> reviews = new ArrayList<>();
for (int i = 0; i < reviewsLength; i++) {
reviews.add(scanner.nextLine());
}
scanner.close();
List<String> res = topMentioned(k, keywords, reviews);
System.out.println(String.join(" ", res));
}
}