problems / 211

Trie-Based Autocomplete (Medium Version)

medium trie design strings 1000 ms · 256 MB

Trie-Based Autocomplete (Medium Version)

This problem extends the easy version (problem 199) by adding a DELETE operation and returning the top-k most frequent words by prefix instead of all words in lexicographic order. You must track word frequencies and support efficient deletion.

Implement an autocomplete system using a trie data structure with frequency tracking. Support three operations: inserting words, deleting words, and finding the top-k most frequently used words with a given prefix.

Input

The first line contains n (1 ≤ n ≤ 3000), the number of operations.

Each of the next n lines contains one operation:

  • INSERT word — add word to the trie, incrementing its frequency by 1. Duplicate inserts increase frequency.
  • DELETE word — decrement the frequency of word by 1. If the frequency drops to zero, the word is removed from the trie. Deleting a word that does not exist or has frequency zero is a no-op (silent failure).
  • AUTOCOMPLETE prefix k — find the k most frequently used words in the trie that start with prefix. If multiple words have the same frequency, return them in lexicographic order. If fewer than k words match the prefix, return all of them.

All words and prefixes are non-empty strings of lowercase English letters, each at most 20 characters. k is between 1 and 100.

Output

For each AUTOCOMPLETE query, print the matching words (up to k most frequent) in the specified order, one per line. If no words match the prefix, print nothing for that query.

After all queries are processed, print the total number of distinct words remaining in the trie (words with frequency ≥ 1) on a single line.

Constraints

  • 1 ≤ n ≤ 3000
  • Each word and prefix is 1–20 lowercase English letters
  • 1 ≤ k ≤ 100
  • Total length of all words and prefixes ≤ 60000
  • Time limit: 1000 ms
  • Memory limit: 256 MB

Notes

The total number of distinct words remaining counts only words with frequency ≥ 1 after all INSERT and DELETE operations.

Example 1

Input

8
INSERT apple
INSERT app
INSERT application
AUTOCOMPLETE app 2
AUTOCOMPLETE app 3
INSERT apple
AUTOCOMPLETE app 10
AUTOCOMPLETE apple 5

Output

app
apple
app
apple
application
apple
app
application
apple
3

Example 2

Input

10
INSERT hello
INSERT hello
INSERT hello
INSERT help
INSERT world
AUTOCOMPLETE hel 2
AUTOCOMPLETE hel 10
AUTOCOMPLETE hel 1
AUTOCOMPLETE w 5
AUTOCOMPLETE w 1

Output

hello
help
hello
help
hello
world
world
3

Hints · reveal one at a time if you're stuck

Hint 1

Each word needs a count that goes up on INSERT and down on DELETE. What should a word whose count reaches 0 look like to a query?

Hint 2

An AUTOCOMPLETE only cares about stored words that start with the prefix. Among those, the answer is the best k by count (highest first), with ties broken alphabetically.

Hint 3

A trie lets a query walk straight to the prefix and visit only the words beneath it, instead of checking every stored word. Store the count at the node for each complete word, and gather the candidates from that subtree.

report an issue Prefer your own editor? sooks prep 211 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.