SashkoKlincharov

[Java][АПС] - Company(Hash)

Jan 23rd, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  4.  
  5. // Each MapEntry object is a pair consisting of a key (a Comparable
  6. // object) and a value (an arbitrary object).
  7. K key;
  8. E value;
  9.  
  10. public MapEntry (K key, E val) {
  11. this.key = key;
  12. this.value = val;
  13. }
  14.  
  15. public int compareTo (K that) {
  16. // Compare this map entry to that map entry.
  17. @SuppressWarnings("unchecked")
  18. MapEntry<K,E> other = (MapEntry<K,E>) that;
  19. return this.key.compareTo(other.key);
  20. }
  21.  
  22. public String toString () {
  23. return key + " = " + value + "\n";
  24. }
  25. }
  26.  
  27.  
  28. class OBHT<K extends Comparable<K>,E> {
  29.  
  30. // An object of class OBHT is an open-bucket hash table, containing entries
  31. // of class MapEntry.
  32. private MapEntry<K,E>[] buckets;
  33.  
  34. // buckets[b] is null if bucket b has never been occupied.
  35. // buckets[b] is former if bucket b is formerly-occupied
  36. // by an entry that has since been deleted (and not yet replaced).
  37.  
  38. static final int NONE = -1; // ... distinct from any bucket index.
  39.  
  40. private static final MapEntry former = new MapEntry(null, null);
  41. // This guarantees that, for any genuine entry e,
  42. // e.key.equals(former.key) returns false.
  43.  
  44. private int occupancy = 0;
  45. // ... number of occupied or formerly-occupied buckets in this OBHT.
  46.  
  47. @SuppressWarnings("unchecked")
  48. public OBHT (int m) {
  49. // Construct an empty OBHT with m buckets.
  50. buckets = (MapEntry<K,E>[]) new MapEntry[m];
  51. }
  52.  
  53.  
  54. private int hash (K key) {
  55. // Translate key to an index of the array buckets.
  56. return Math.abs(key.hashCode()) % buckets.length;
  57. }
  58.  
  59. public MapEntry<K,E> getAtIndex(int index){
  60. return buckets[index];
  61. }
  62. public int search (K targetKey) {
  63. // Find which if any bucket of this OBHT is occupied by an entry whose key
  64. // is equal to targetKey. Return the index of that bucket.
  65. int b = hash(targetKey); int n_search=0;
  66. for (;;) {
  67. MapEntry<K,E> oldEntry = buckets[b];
  68. if (oldEntry == null)
  69. return NONE;
  70. else if (targetKey.equals(oldEntry.key))
  71. return b;
  72. else
  73. {
  74. b = (b + 1) % buckets.length;
  75. n_search++;
  76. if(n_search==buckets.length)
  77. return NONE;
  78.  
  79. }
  80. }
  81. }
  82.  
  83.  
  84. public void insert (K key, E val) {
  85. // Insert the entry <key, val> into this OBHT.
  86. MapEntry<K,E> newEntry = new MapEntry<K,E>(key, val);
  87. int b = hash(key); int n_search=0;
  88. for (;;) {
  89. MapEntry<K,E> oldEntry = buckets[b];
  90. if (oldEntry == null) {
  91. if (++occupancy == buckets.length) {
  92. System.out.println("Hash tabelata e polna!!!");
  93. }
  94. buckets[b] = newEntry;
  95. return;
  96. }
  97. else if (oldEntry == former
  98. || key.equals(oldEntry.key)) {
  99. buckets[b] = newEntry;
  100. return;
  101. }
  102. else
  103. {
  104. b = (b + 1) % buckets.length;
  105. n_search++;
  106. if(n_search==buckets.length)
  107. return;
  108.  
  109. }
  110. }
  111. }
  112.  
  113.  
  114. @SuppressWarnings("unchecked")
  115. public void delete (K key) {
  116. // Delete the entry (if any) whose key is equal to key from this OBHT.
  117. int b = hash(key); int n_search=0;
  118. for (;;) {
  119. MapEntry<K,E> oldEntry = buckets[b];
  120.  
  121. if (oldEntry == null)
  122. return;
  123. else if (key.equals(oldEntry.key)) {
  124. buckets[b] = former;//(MapEntry<K,E>)former;
  125. return;
  126. }
  127. else{
  128. b = (b + 1) % buckets.length;
  129. n_search++;
  130. if(n_search==buckets.length)
  131. return;
  132.  
  133. }
  134. }
  135. }
  136.  
  137.  
  138. public String toString () {
  139. String temp = "";
  140. for (int i = 0; i < buckets.length; i++) {
  141. if (buckets[i] == null)
  142. temp += "\n";
  143. else if (buckets[i] == former)
  144. temp += "former\n";
  145. else
  146. temp += buckets[i] + "\n";
  147. }
  148. return temp;
  149. }
  150.  
  151.  
  152. public OBHT<K,E> clone () {
  153. OBHT<K,E> copy = new OBHT<K,E>(buckets.length);
  154. for (int i = 0; i < buckets.length; i++) {
  155. MapEntry<K,E> e = buckets[i];
  156. if (e != null && e != former)
  157. copy.buckets[i] = new MapEntry<K,E>(e.key, e.value);
  158. else
  159. copy.buckets[i] = e;
  160. }
  161. return copy;
  162. }
  163. }
  164. public class Company{
  165.  
  166. public static void main(String [] args) {
  167. Scanner input = new Scanner(System.in);
  168. int n = input.nextInt();
  169. input.nextLine();
  170.  
  171. OBHT<String,Integer> hashtable = new OBHT<String,Integer>(n*2+1);
  172. for(int i=0;i<n;i++) {
  173. String [] line = input.nextLine().split(" ");
  174. String employee = line[0];
  175. String manager = line[1];
  176.  
  177. int index = hashtable.search(manager);
  178.  
  179. if(index>=0) {
  180. MapEntry<String,Integer> me = hashtable.getAtIndex(index);
  181. if(!me.key.equals(employee)) {
  182. int broj = me.value;
  183. ++broj;
  184. hashtable.insert(manager, broj);
  185. }
  186. else {}
  187. }else {
  188. hashtable.insert(manager, 1);
  189. }
  190. }
  191. System.out.println(hashtable.toString());
  192. }
  193. }
Add Comment
Please, Sign In to add comment