#K90377. Simple Hash Table Implementation
Simple Hash Table Implementation
Simple Hash Table Implementation
You are required to implement a simple hash table that supports three operations: insert, delete, and search. The table is initially empty.
The operations are defined as follows:
- insert key value: Insert a key-value pair into the hash table. If the key already exists, update its value using the rule \( v_{new} = v_{old} + value \).
- delete key: Remove the key (and its corresponding value) from the hash table if it exists.
- search key: Print the value associated with the given key. If the key does not exist, print -1.
The input is read from standard input and the output is printed to standard output.
inputFormat
The first line contains an integer n (\(1 \leq n \leq 10^5\)) representing the number of operations. The next n lines describe the operations. Each operation is in one of the following forms:
insert key value
— where key is a string and value is an integer.delete key
— where key is a string.search key
— where key is a string.
outputFormat
For every search
operation, output the result (the corresponding value or -1 if the key does not exist) on a new line.
4
insert apple 10
search apple
insert apple 5
search apple
10
15
</p>