This commit is contained in:
2026-04-27 20:53:53 +03:00
parent c71becc3d6
commit 9d84c20591
30 changed files with 384 additions and 0 deletions

52
1/A/Main.java Normal file
View 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);
}
}

11
1/A/test1.in Normal file
View File

@@ -0,0 +1,11 @@
4 5
peterburg
murmansk
perm
moscow
p
m
m
p
p

10
1/A/test2.in Normal file
View File

@@ -0,0 +1,10 @@
5 3
int
main
void
double
string
v
m
v

6
1/A/test3.in Normal file
View File

@@ -0,0 +1,6 @@
1 3
python
p
p
p