Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 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.  
  7. package rutirackatabela_2;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12.  
  13. /**
  14. *
  15. * @author eli
  16. */
  17. class SLLNode<E> {
  18. protected E element;
  19. protected SLLNode<E> succ;
  20.  
  21. public SLLNode(E elem, SLLNode<E> succ) {
  22. this.element = elem;
  23. this.succ = succ;
  24. }
  25.  
  26. @Override
  27. public String toString() {
  28. return element.toString();
  29. }
  30. }
  31.  
  32. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  33.  
  34. // Each MapEntry object is a pair consisting of a key (a Comparable
  35. // object) and a value (an arbitrary object).
  36. K key;
  37. E value;
  38.  
  39. public MapEntry (K key, E val) {
  40. this.key = key;
  41. this.value = val;
  42. }
  43.  
  44. public int compareTo (K that) {
  45. // Compare this map entry to that map entry.
  46. @SuppressWarnings("unchecked")
  47. MapEntry<K,E> other = (MapEntry<K,E>) that;
  48. return this.key.compareTo(other.key);
  49. }
  50.  
  51. public String toString () {
  52. return "<" + key + "," + value + ">";
  53. }
  54. }
  55.  
  56. class CBHT<K extends Comparable<K>, E> {
  57.  
  58. // An object of class CBHT is a closed-bucket hash table, containing
  59. // entries of class MapEntry.
  60. private SLLNode<MapEntry<K,E>>[] buckets;
  61.  
  62. @SuppressWarnings("unchecked")
  63. public CBHT(int m) {
  64. // Construct an empty CBHT with m buckets.
  65. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  66. }
  67.  
  68. private int hash(K key) {
  69. // Translate key to an index of the array buckets.
  70. return Math.abs(key.hashCode()) % buckets.length;
  71. }
  72.  
  73. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  74. // Find which if any node of this CBHT contains an entry whose key is
  75. // equal
  76. // to targetKey. Return a link to that node (or null if there is none).
  77. int b = hash(targetKey);
  78. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  79. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  80. return curr;
  81. }
  82. return null;
  83. }
  84.  
  85. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  86. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  87. int b = hash(key);
  88. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  89. if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  90. // Make newEntry replace the existing entry ...
  91. curr.element = newEntry;
  92. return;
  93. }
  94. }
  95. // Insert newEntry at the front of the 1WLL in bucket b ...
  96. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  97. }
  98.  
  99. public void delete(K key) {
  100. // Delete the entry (if any) whose key is equal to key from this CBHT.
  101. int b = hash(key);
  102. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  103. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  104. if (pred == null)
  105. buckets[b] = curr.succ;
  106. else
  107. pred.succ = curr.succ;
  108. return;
  109. }
  110. }
  111. }
  112.  
  113. public String toString() {
  114. String temp = "";
  115. for (int i = 0; i < buckets.length; i++) {
  116. temp += i + ":";
  117. for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  118. temp += curr.element.toString() + " ";
  119. }
  120. temp += "\n";
  121. }
  122. return temp;
  123. }
  124.  
  125. }
  126.  
  127. public class RutirackaTabela_2 {
  128.  
  129. /**
  130. * @param args the command line arguments
  131. */
  132. public static void main(String[] args) throws IOException {
  133. // TODO code application logic here
  134.  
  135. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  136. int N=Integer.parseInt(br.readLine());
  137.  
  138. CBHT<String,String[]> tabela=new CBHT<>(2*N);
  139. for(int i=0;i<N;i++)
  140. {
  141. String kluc=br.readLine();
  142. String [] vrednost=br.readLine().split(" ");
  143.  
  144. tabela.insert(kluc, vrednost);
  145. }
  146.  
  147. int M=Integer.parseInt(br.readLine());
  148. for(int j=0;j<M;j++)
  149. {
  150. String kluc2=br.readLine();
  151. String vrednost2=br.readLine();
  152. String ip=vrednost2.substring(0,vrednost2.lastIndexOf("."));
  153.  
  154. SLLNode<MapEntry<String,String[]>> node=tabela.search(kluc2);
  155. if(node==null)
  156. {
  157. System.out.println("/");
  158. }
  159. else
  160. {
  161. //bidejkji node.value.element mi e niza go stavam prvin vo promenliva, pa posle pravams ubstring za proverka
  162. String [] pomNiza=node.element.value;
  163.  
  164. for(int i=0;i<pomNiza.length;i++)
  165. {
  166. if(pomNiza[i].substring(0, pomNiza[i].lastIndexOf(".")).equals(ip))
  167. {
  168. System.out.println("postoi");
  169. }
  170. else System.out.println("/");
  171. }
  172.  
  173.  
  174.  
  175. }
  176. }
  177.  
  178.  
  179. }
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement