All problems

Problem 739

Implement Trie II

Medium · 20 pts · Design, Trie, String

Problem

Build a trie that counts words. 'insert w' adds one copy of the word w (the same word can be inserted several times). 'countEqual w' prints how many copies of w are stored. 'countPrefix p' prints how many stored words (counting copies) start with p. 'erase w' removes one copy of w; it is only called when w is stored. Keep two counters in every trie node: how many words pass through it and how many end at it.

Input

The first line contains Q. Each of the next Q lines contains one operation. Words use lowercase letters. There is at least one count operation.

Output

Print the result of every countEqual and countPrefix, one per line.

Constraints

1 <= Q <= 30000; 1 <= |w|, |p| <= 1000

Example input

7
insert apple
insert apple
countEqual apple
countPrefix app
insert app
erase apple
countPrefix app

Example output

2
2
2

Explanation

After inserting apple twice, countEqual apple is 2 and countPrefix app is 2. After inserting app and erasing one apple, countEqual apple is 1 and countPrefix app is 2 (one apple and one app).

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Page Faults in LRU Next: Complete String