update
This commit is contained in:
52
1/A/Main.java
Normal file
52
1/A/Main.java
Normal file
@@ -0,0 +1,52 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
StringTokenizer st = new StringTokenizer(br.readLine());
|
||||
int n = Integer.parseInt(st.nextToken());
|
||||
int k = Integer.parseInt(st.nextToken());
|
||||
|
||||
Map<Character, TreeMap<Integer, TreeSet<String>>> index = new HashMap<>();
|
||||
Map<String, Integer> counts = new HashMap<>();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
String word = br.readLine().trim();
|
||||
char c = word.charAt(0);
|
||||
counts.put(word, 0);
|
||||
index
|
||||
.computeIfAbsent(c, x -> new TreeMap<>())
|
||||
.computeIfAbsent(0, x -> new TreeSet<>())
|
||||
.add(word);
|
||||
}
|
||||
|
||||
for (int i = 0; i < k; i++) {
|
||||
String letter = br.readLine().trim();
|
||||
char c = letter.charAt(0);
|
||||
|
||||
TreeMap<Integer, TreeSet<String>> byCount = index.get(c);
|
||||
|
||||
Map.Entry<Integer, TreeSet<String>> minEntry = byCount.firstEntry();
|
||||
int minCount = minEntry.getKey();
|
||||
TreeSet<String> minWords = minEntry.getValue();
|
||||
String bestWord = minWords.first();
|
||||
|
||||
sb.append(bestWord).append('\n');
|
||||
|
||||
minWords.remove(bestWord);
|
||||
if (minWords.isEmpty()) {
|
||||
byCount.remove(minCount);
|
||||
}
|
||||
int newCount = minCount + 1;
|
||||
byCount
|
||||
.computeIfAbsent(newCount, x -> new TreeSet<>())
|
||||
.add(bestWord);
|
||||
counts.put(bestWord, newCount);
|
||||
}
|
||||
|
||||
System.out.print(sb);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user