Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class CBHT<K extends Comparable<K>, E> {
  6.  
  7. // An object of class CBHT is a closed-bucket hash table, containing
  8. // entries of class MapEntry.
  9. private SLLNode<MapEntry<K,E>>[] buckets;
  10.  
  11. @SuppressWarnings("unchecked")
  12. public CBHT(int m) {
  13. // Construct an empty CBHT with m buckets.
  14. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  15. }
  16.  
  17. private int hash(K key) {
  18. // Translate key to an index of the array buckets.
  19. return Math.abs(key.hashCode()) % buckets.length;
  20. }
  21.  
  22. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  23. // Find which if any node of this CBHT contains an entry whose key is
  24. // equal
  25. // to targetKey. Return a link to that node (or null if there is none).
  26. int b = hash(targetKey);
  27. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  28. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  29. return curr;
  30. }
  31. return null;
  32. }
  33.  
  34. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  35. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  36. int b = hash(key);
  37. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  38. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  39. // Make newEntry replace the existing entry ...
  40. curr.element = newEntry;
  41. return;
  42. }
  43. }
  44. // Insert newEntry at the front of the 1WLL in bucket b ...
  45. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  46. }
  47.  
  48. public void delete(K key) {
  49. // Delete the entry (if any) whose key is equal to key from this CBHT.
  50. int b = hash(key);
  51. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  52. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  53. if (pred == null)
  54. buckets[b] = curr.succ;
  55. else
  56. pred.succ = curr.succ;
  57. return;
  58. }
  59. }
  60. }
  61.  
  62. public String toString() {
  63. String temp = "";
  64. for (int i = 0; i < buckets.length; i++) {
  65. temp += i + ":";
  66. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  67. temp += curr.element.toString() + " ";
  68. }
  69. temp += "\n";
  70. }
  71. return temp;
  72. }
  73.  
  74. }
  75.  
  76. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  77.  
  78. // Each MapEntry object is a pair consisting of a key (a Comparable
  79. // object) and a value (an arbitrary object).
  80. K key;
  81. E value;
  82.  
  83. public MapEntry (K key, E val) {
  84. this.key = key;
  85. this.value = val;
  86. }
  87.  
  88. public int compareTo (K that) {
  89. // Compare this map entry to that map entry.
  90. @SuppressWarnings("unchecked")
  91. MapEntry<K,E> other = (MapEntry<K,E>) that;
  92. return this.key.compareTo(other.key);
  93. }
  94.  
  95. public String toString () {
  96. return "<" + key + "," + value + ">";
  97. }
  98. }
  99.  
  100. class SLLNode<E> {
  101. protected E element;
  102. protected SLLNode<E> succ;
  103.  
  104. public SLLNode(E elem, SLLNode<E> succ) {
  105. this.element = elem;
  106. this.succ = succ;
  107. }
  108.  
  109. @Override
  110. public String toString() {
  111. return element.toString();
  112. }
  113. public E getElement(){
  114. return element;
  115. }
  116. public void setElement(E element){
  117. this.element=element;
  118. }
  119. }
  120. public class Lozinki {
  121. public static void main (String[] args) throws IOException {
  122. CBHT<String,String> tabela;
  123. BufferedReader br = new BufferedReader(new InputStreamReader(
  124. System.in));
  125. int N = Integer.parseInt(br.readLine());
  126. tabela = new CBHT<String,String>(2*N-1);
  127. for(int i=0;i<N;i++){
  128. String imelozinka = br.readLine();
  129. String [] pom = imelozinka.split(" ");
  130. tabela.insert(pom[0], pom[1]);
  131. }
  132. //System.out.print(tabela);
  133. while(true){
  134. String line=br.readLine();
  135. if(line.equals("KRAJ"))break;
  136. String [] parts=line.split(" ");
  137. SLLNode<MapEntry<String,String>> temp=tabela.search(parts[0]);
  138. if(temp==null||!(temp.getElement().value.equals(parts[1])))
  139. System.out.println("Nenajaven");
  140. else
  141. {
  142. System.out.println("Najaven");
  143. break;
  144. }
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement