Problem:
Design RandomPool structure
[Title] design a structure has the following three functions in the structure:
INSERT (key): the addition of a key to the structure, do not repeat added.
delete (key): remove the original structure of a key.
getRandom (): returns the probability of any other key structures.
[Required] Insert, delete and getRandom time complexity methods are O (1)
Solution:
Use two hash table is a record label, a record keyword
here is a key point is that the probability of return such as a keyword
if using a simple hash table for storage, then there is a problem when deleting data , the hash table will be generated such that the intermediate blank data
is preferably generated null data to avoid the intermediate approach is to delete the data to be exchanged with the data in the table at the end
and then remove the last data directly, it is necessary to use two hash tables
Code:
1 #pragma once 2 #include <iostream> 3 #include <hash_map> 4 #include <string> 5 #include <time.h> 6 #include <stdlib.h> 7 8 using namespace std; 9 10 template<class T> 11 class RandomPool 12 { 13 public: 14 void insert(T key); 15 void del(T key); 16 T getRandom(); 17 void getPrint(T key); 18 void getPrint(int index); 19 20 private: 21 hash_map<T, int>KeyMap; 22 hash_map<int, T>IndexMap; 23 int size = 0; 24 }; 25 26 27 template<class T> 28 void RandomPool<T>::insert(T key) 29 { 30 if (KeyMap.find(key) == KeyMap.end()) 31 { 32 KeyMap[key] = this->size; 33 IndexMap[this->size] = key; 34 ++(this->size); 35 cout << "add succeed!" << endl; 36 } 37 else 38 cout << "add filed!" << endl; 39 } 40 41 42 template<class T> 43 void RandomPool<T>::del(T key) 44 is { 45 Auto PTR = KeyMap.find (Key); 46 is IF (PTR == KeyMap.end ()) 47 { 48 COUT << " !! Filed there IS Not Delete Key ExSite The " << endl; 49 return ; 50 } 51 // switching elements to find the last element 52 is T IndexMap TEMP = [- ( the this -> size)]; // keyword last element, while the elements of the hash table is deleted 53 is int index KEYMAP = [Key]; // to delete the position of the element 54 is KEYMAP [TEMP] = index; 55 IndexMap [index] = TEMP; // the last element To delete a position of the element 56 // actually deleted 57 is KeyMap.erase (PTR); 58 IndexMap.erase (IndexMap.find (index)); 59 } 60 61 is Template < class T> 62 is T RandomPool <T> :: getRandom () 63 is { 64 IF ( the this -> size == 0 ) 65 { 66 COUT << " The Map IS empty! " << endl; 67 } 68 the else 69 { 70 int index = ( int ) ((RAND ()% ( 99 + . 1 ) / ( Double ) ( 99 + . 1 )) * ( the this -> size)); // generate a random position 71 is return IndexMap [index ]; 72 } 73 is } 74 75 Template < class T> 76 void RandomPool <T> :: getPrint (T Key) 77 { 78 IF (KeyMap.find (Key) == KeyMap.end ()) 79 COUT <<"the key is not exsite!" << endl; 80 else 81 cout << KeyMap[key] << endl; 82 } 83 84 template<class T> 85 void RandomPool<T>::getPrint(int index) 86 { 87 if (IndexMap.find(index) == IndexMap.end()) 88 cout << "the key is not exsite!" << endl; 89 else 90 cout << IndexMap[index] << endl; 91 } 92 93 94 void Test() 95 { 96 srand((unsigned)time(NULL)); 97 RandomPool<string>map; 98 map.insert("zz"); 99 map.insert("zw"); 100 map.insert("ww"); 101 map.insert("wz"); 102 103 cout << map.getRandom() << endl; 104 map.getPrint(2); 105 map.getPrint("ww"); 106 map.del("zw"); 107 map.getPrint("zw"); 108 }