Advertisement
Guest User

Untitled

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