Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class SLLNode<E>{
  6. private E info;
  7. private SLLNode<E> link;
  8.  
  9. public SLLNode(E info, SLLNode<E> link)
  10. {
  11. this.info=info;
  12. this.link=link;
  13. }
  14. public E getInfo()
  15. {
  16. return info;
  17. }
  18. public SLLNode<E> getLink()
  19. {
  20. return link;
  21. }
  22. public void setInfo(E info)
  23. {
  24. this.info=info;
  25. }
  26. public void setLink(SLLNode<E> link)
  27. {
  28. this.link=link;
  29. }
  30. }
  31.  
  32. class MapEntry<K extends Comparable<K>, E> implements Comparable<K>{
  33. private K key;
  34. private E value;
  35.  
  36. public MapEntry(K key, E value)
  37. {
  38. this.key=key;
  39. this.value=value;
  40. }
  41. public String toString()
  42. {
  43. return String.format("<%s, %s>", key, value);
  44. }
  45. @Override
  46. public int compareTo(K k) {
  47. MapEntry<K, E> me=(MapEntry<K, E>) k;
  48.  
  49. return this.key.compareTo(me.key);
  50. }
  51. public K getKey()
  52. {
  53. return key;
  54. }
  55. public E getValue()
  56. {
  57. return value;
  58. }
  59. }
  60.  
  61. class CBHT<K extends Comparable<K>, E>{
  62. private SLLNode<MapEntry<K, E>> []buckets;
  63.  
  64. public CBHT(int m)
  65. {
  66. buckets=(SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  67. }
  68. private int hash(K key)
  69. {
  70. return Math.abs(key.hashCode()) % buckets.length;
  71. }
  72. public SLLNode<MapEntry<K, E>> search(K targetKey)
  73. {
  74. int b=this.hash(targetKey);
  75.  
  76. for(SLLNode<MapEntry<K, E>> curr=buckets[b]; curr!=null; curr=curr.getLink())
  77. if(targetKey.equals(((MapEntry<K, E>) curr.getInfo()).getKey()))
  78. return curr;
  79.  
  80. return null;
  81. }
  82. public void insert(K key, E value)
  83. {
  84. MapEntry<K, E> ins=new MapEntry(key, value);
  85. int b=hash(key);
  86.  
  87. for(SLLNode<MapEntry<K, E>> curr=buckets[b]; curr!=null; curr=curr.getLink())
  88. {
  89. if (key.equals(((MapEntry<K, E>) curr.getInfo()).getKey()))
  90. {
  91. curr.setInfo(ins);
  92. return;
  93. }
  94. }
  95. buckets[b]=new SLLNode<MapEntry<K,E>>(ins, buckets[b]);
  96. }
  97. public void delete(K key)
  98. {
  99. int b=hash(key);
  100.  
  101. for(SLLNode<MapEntry<K, E>> curr=buckets[b], pred=null; curr!=null; pred=curr, curr=curr.getLink())
  102. if(key.equals(((MapEntry<K, E>) curr.getInfo()).getKey()))
  103. {
  104. if(pred==null)
  105. buckets[b]=curr.getLink();
  106. else
  107. pred.setLink(curr.getLink());
  108. return;
  109. }
  110. }
  111. public String toString()
  112. {
  113. StringBuilder sb=new StringBuilder();
  114.  
  115. for(int i=0; i<buckets.length; i++)
  116. {
  117. sb.append(i +":");
  118. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.getLink())
  119. sb.append(curr.getInfo().toString() +" ");
  120. sb.append("\n");
  121. }
  122. return sb.toString();
  123. }
  124. }
  125.  
  126. public class Lozinki {
  127. public static void main (String[] args) throws IOException {
  128. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  129. int N = Integer.parseInt(br.readLine());
  130.  
  131. CBHT<String, String> hash=new CBHT(N);
  132. String s;
  133. String []ss;
  134. for(int i=1;i<=N;i++){
  135. String imelozinka = br.readLine();
  136. String[] pom = imelozinka.split(" ");
  137.  
  138. hash.insert(pom[1], pom[0]);
  139. }
  140.  
  141. while(true)
  142. {
  143. s=br.readLine();
  144.  
  145. if(s.equals("KRAJ"))
  146. break;
  147.  
  148. ss=s.split(" ");
  149.  
  150. if(hash.search(ss[1])==null)
  151. System.out.println("Nenajaven");
  152. else
  153. {
  154. if(ss[0].equals(hash.search(ss[1]).getInfo().getValue()))
  155. {
  156. System.out.println("Najaven");
  157. break;
  158. }
  159. else
  160. System.out.println("Nenajaven");
  161. }
  162. }
  163.  
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement