Tous les défis
Moyen 0 résolus 0 % de réussite

Byte Pair Encoding (BPE) Merges

Learn subword merges the way GPT tokenizers do: repeatedly merge the most frequent pair of adjacent symbols.

Ce que vous allez apprendre

  • Count pairs of adjacent symbols
  • Merge the most frequent pair repeatedly
  • Understand how GPT-style tokenizers are built

Problème

Afficher la traduction

GPT-style tokenizers use Byte Pair Encoding: start from single characters and repeatedly merge the most common pair of neighbors into a new symbol.

  1. Split every word in words into characters. Repeated words count multiple times.
  2. Count every pair of adjacent symbols across all words.
  3. Pick the most frequent pair. If pairs tie, pick the one that is smallest when you compare (first symbol, second symbol) as strings.
  4. Replace every occurrence of that pair, left to right, with the merged symbol, and record the merged symbol.
  5. Repeat num_merges times, or stop early when no word has two symbols left.

Implement bpe_merges(words, num_merges).

Input

words: The training words (lowercase letters).

num_merges: How many merges to learn.

Output

Return the merged symbols in the order they were learned.

Contraintes

1 ≤ number of words ≤ 1,000, 1 ≤ length of each word ≤ 30

0 ≤ num_merges ≤ 200

Exemples

Exemple 1
Entrée :
words = ["low", "lower", "lowest"]
num_merges = 2

Sortie attendue :
expected = ["lo", "low"]
Entrée :
std::vector<std::string> words = {"low", "lower", "lowest"};
long long num_merges = 2;

Sortie attendue :
std::vector<std::string> expected = {"lo", "low"};
Entrée :
string[] words = new string[] {"low", "lower", "lowest"};
long num_merges = 2;

Sortie attendue :
string[] expected = new string[] {"lo", "low"};

Explication

Pairs: "l o" and "o w" appear 3 times each; the tie goes to "l o" ("l" < "o"), giving "lo". Now "lo w" appears 3 times, giving "low". Result: ["lo", "low"].

Exemple 2
Entrée :
words = ["aaa"]
num_merges = 3

Sortie attendue :
expected = ["aa", "aaa"]
Entrée :
std::vector<std::string> words = {"aaa"};
long long num_merges = 3;

Sortie attendue :
std::vector<std::string> expected = {"aa", "aaa"};
Entrée :
string[] words = new string[] {"aaa"};
long num_merges = 3;

Sortie attendue :
string[] expected = new string[] {"aa", "aaa"};

Explication

"aaa" has the pair "a a" twice. Merging left to right gives "aa" + "a". Then "aa a" merges into "aaa", and the word is a single symbol, so the third merge has nothing to do. Result: ["aa", "aaa"].

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Track the current symbol sequence for each word and repeatedly count neighboring pairs across all repeated words.

Indice 2

Use pair counting plus deterministic tie-breaking by comparing the pair as two strings, and then update every word left to right.

Indice 3

Watch words that shrink to one symbol; when no adjacent pair exists anywhere, stop even if merges remain.

Complexité attendue

O(num_merges * total_symbols) time, O(total_symbols) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.