Advertisement
Guest User

1va

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