eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Cuckoo for Hashing

Cuckoo for Hashing

An integer hash table is a data structure that supports insert, delete and lookup of integer values in constant time. Traditional hash structures consist of an array (the \textit{hash table}) of some size \textbf{n}, and a \textit{hash function} \textbf{f(x)} which is typically \textbf{f(x) = x mod n}. To insert a value \textbf{x} into the table, you compute its \textit{hash value} \textbf{f(x)} which serves as an index into the hash table for the location to store \textbf{x}. For example, if \textbf{x = 1234} and the hash table has size \textbf{101}, then \textbf{1234} would be stored in location \textbf{22 = 1234 mod 101}. Of course, it's possible that some other value is already stored in location \textbf{22} (\textbf{x = 22} for example), which leads to a collision. Collisions can be handled in a variety of ways which you can discuss with your faculty advisor on the way home from the contest. Cuckoo hashing is a form of hashing that employs two hash tables \textbf{T_1} and \textbf{T_2}, each with its own hash function \textbf{f_1(x) }and \textbf{f_2(x)}. Insertion of a value \textbf{x} proceeds as follows: you first try to store \textbf{x} in \textbf{T_1} using \textbf{f_1(x)}. If that location is empty, then simply store \textbf{x} there and you're done. Otherwise there is a collision which must be handled. Let \textbf{y} be the value currently in that location. You replace \textbf{y} with \textbf{x} in \textbf{T_1}, and then try to store \textbf{y} in \textbf{T_2} using \textbf{f_2(y)}. Again, if this location is empty, you store y there and you're done. Otherwise, replace the value there (call it \textbf{z}) with \textbf{y}, and now try to store \textbf{z}back in \textbf{T_1} using \textbf{f_1(z)}, and so on. This continues, bouncing back and forth between the two tables until either you find an empty location, or until a certain number of swaps have occurred, at which point you rehash both tables (again, something to discuss with your faculty advisor). For the purposes of this problem, this latter occurrence will never happen, i.e., the process should always continue until an empty location is found, which will be guaranteed to happen for each inserted value. Given the size of the two tables and a series of insertions, your job is to determine what is stored in each of the tables. (For those interested, cuckoo hashing gets its name from the behavior of the cuckoo bird, which is known to y to other bird's nests and lay its own eggs in it alongside the eggs already there. When the larger cuckoo chick hatches, it pushes the other chicks out of the nest, thus getting all the food for itself. Gruesome but efficient.) \InputFile Input for each test case starts with \textbf{3} positive integers \textbf{n_1 n_2 m}, where \textbf{n_1} and \textbf{n_2} are the sizes of the tables \textbf{T_1} and \textbf{T_\{2 \}}(with \textbf{n_1}, \textbf{n_2} ≤ \textbf{1000} and \textbf{n_1} ≠ \textbf{n_2}) and \textbf{m} is the number of inserts. Following this will be \textbf{m} integer values which are the values to be inserted into the tables. All of these values will be non-negative. Each table is initially empty, and table \textbf{T_\{i \}}uses the hash function \textbf{f_i(x) = x mod n_i}. A line containing \textbf{3} zeros will terminate input. \OutputFile For each test case, output the non-empty locations in \textbf{T_1} followed by the non-empty locations in \textbf{T_2}. Use one line for each such location and the form \textbf{i:v}, where \textbf{i} is the index location of the table, and \textbf{v} is the value stored there. Output values in each table from lowest index to highest. If either table is empty, output nothing for that table.
Лимит времени 1 секунда
Лимит использования памяти 64 MiB
Входные данные #1
5 7 4
8 18 29 4
6 7 4
8 18 29 4
1000 999 2
1000
2000
0 0 0
Выходные данные #1
Case 1:
Table 1
3:8
4:4
Table 2
1:29
4:18
Case 2:
Table 1
0:18
2:8
4:4
5:29
Case 3:
Table 1
0:2000
Table 2
1:1000
Источник ACM ICPC East Central North America Region 2013