Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package lozinki1;
  7.  
  8. /**
  9. *
  10. * @author GORAN
  11. */
  12. import java.io.BufferedReader;
  13. import java.io.IOException;
  14. import java.io.InputStreamReader;
  15.  
  16. class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
  17.  
  18. K key;
  19. E value;
  20.  
  21. public MapEntry(K key, E val) {
  22. this.key = key;
  23. this.value = val;
  24. }
  25.  
  26. public int compareTo(K that) {
  27. @SuppressWarnings("unchecked")
  28. MapEntry<K, E> other = (MapEntry<K, E>) that;
  29. return this.key.compareTo(other.key);
  30. }
  31.  
  32. public String toString() {
  33. return "<" + key + "," + value + ">";
  34. }
  35. }
  36.  
  37. class SLLNode<E> {
  38.  
  39. protected E element;
  40. protected SLLNode<E> succ;
  41.  
  42. public SLLNode(E elem, SLLNode<E> succ) {
  43. this.element = elem;
  44. this.succ = succ;
  45. }
  46.  
  47. @Override
  48. public String toString() {
  49. return element.toString();
  50. }
  51. }
  52.  
  53. class CBHT<K extends Comparable<K>, E> {
  54.  
  55. private SLLNode<MapEntry<K, E>>[] buckets;
  56.  
  57. @SuppressWarnings("unchecked")
  58. public CBHT(int m) {
  59. buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
  60. }
  61.  
  62. private int hash(K key) {
  63. return Math.abs(key.hashCode()) % buckets.length;
  64. }
  65.  
  66. public SLLNode<MapEntry<K, E>> search(K targetKey) {
  67. int b = hash(targetKey);
  68. for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  69. if (targetKey.equals(((MapEntry<K, E>) curr.element).key)) {
  70. return curr;
  71. }
  72. }
  73. return null;
  74. }
  75.  
  76. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  77. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  78. int b = hash(key);
  79. for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  80. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  81. curr.element = newEntry;
  82. return;
  83. }
  84. }
  85. buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  86. }
  87.  
  88. public void delete(K key) {
  89. int b = hash(key);
  90. for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  91. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  92. if (pred == null) {
  93. buckets[b] = curr.succ;
  94. } else {
  95. pred.succ = curr.succ;
  96. }
  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. public class Lozinki1 {
  117.  
  118. public static void main(String[] args) throws IOException {
  119. CBHT<String,String> tabela;
  120. BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
  121. String s=stdin.readLine();
  122. int N=Integer.parseInt(s);
  123. tabela=new CBHT<>(N);
  124. for (int i=0;i<N;i++){
  125. String korisnik=stdin.readLine();
  126. String[] b=korisnik.split(" ");
  127. tabela.insert(b[0], b[1]);
  128. }
  129.  
  130. String najava=stdin.readLine();
  131. while(!najava.equals("KRAJ")){
  132. String[] b=najava.split(" ");
  133. String kluc=b[0];
  134. String vrednost=b[1];
  135. SLLNode<MapEntry<String,String>> jazol=tabela.search(kluc);
  136. if(jazol!=null){
  137. if(jazol.element.value.equals(vrednost)){
  138. System.out.println("Najaven");
  139. }
  140. else{
  141. System.out.println("Nenajaven");
  142. }
  143. }
  144. else{
  145. System.out.println("Nenajaven");
  146. }
  147. najava=stdin.readLine();
  148.  
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement