Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class RoutingHashJava {
- class SLLNode<E> {
- protected E element;
- protected SLLNode<E> succ;
- public SLLNode(E elem, SLLNode<E> succ) {
- this.element = elem;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- // MAP
- class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
- // Each MapEntry object is a pair consisting of a key (a Comparable
- // object) and a value (an arbitrary object).
- K key;
- E value;
- public MapEntry(K key, E val) {
- this.key = key;
- this.value = val;
- }
- public int compareTo(K that) {
- return this.key.compareTo(that);
- }
- public String toString() {
- return "<" + key + "," + value + ">";
- }
- }
- // CBHT
- class CBHT<K extends Comparable<K>, E> {
- // An object of class CBHT is a closed-bucket hash table, containing
- // entries of class MapEntry.
- private SLLNode<MapEntry<K, E>>[] buckets;
- @SuppressWarnings("unchecked")
- public CBHT(int m) {
- // Construct an empty CBHT with m buckets.
- buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
- }
- private int hash(K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public SLLNode<MapEntry<K, E>> search(K targetKey) {
- // Find which if any node of this CBHT contains an entry whose key
- // is
- // equal
- // to targetKey. Return a link to that node (or null if there is
- // none).
- int b = hash(targetKey);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (targetKey.compareTo(curr.element.key) == 0)
- return curr;
- }
- return null;
- }
- public void insert(K key, E val) { // Insert the entry <key, val> into
- // this
- // CBHT.
- MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
- int b = hash(key);
- for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
- if (key.equals(curr.element.key)) {
- // Make newEntry replace the existing entry ...
- curr.element = newEntry;
- return;
- }
- }
- // Insert newEntry at the front of the 1WLL in bucket b ...
- buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
- }
- public void delete(K key) {
- // Delete the entry (if any) whose key is equal to key from this
- // CBHT.
- int b = hash(key);
- for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
- if (key.equals(((MapEntry<K, E>) curr.element).key)) {
- if (pred == null)
- buckets[b] = curr.succ;
- else
- pred.succ = curr.succ;
- return;
- }
- }
- }
- public String toString() {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
- temp += curr.element.toString() + " ";
- }
- temp += "\n";
- }
- return temp;
- }
- public boolean ima(K key, String val) {
- SLLNode<MapEntry<K, E>> tmp = search(key);
- if (tmp != null) {
- IPs adresi = (IPs) tmp.element.value;
- if (adresi.postoi(val))
- return true;
- else
- return false;
- } else
- return false;
- }
- }
- class Ruter implements Comparable<Ruter> {
- protected String adresa;
- Ruter(String s) {
- adresa = s;
- }
- @Override
- public int hashCode() {
- int sum = 0;
- for (int i = 0; i < adresa.length(); i++)
- sum += adresa.charAt(i);
- return sum;
- }
- public int compareTo(Ruter r) {
- if (adresa.equals(r.adresa))
- return 0;
- else
- return -1;
- }
- }
- class IPs {
- protected ArrayList<String> lista;
- IPs() {
- lista = new ArrayList<String>();
- }
- public boolean postoi(String s) {
- int ind = s.lastIndexOf('.');
- s = s.substring(0, ind);
- for (int i = 0; i < lista.size(); i++) {
- String nov = lista.get(i);
- int indd = nov.lastIndexOf('.');
- nov = nov.substring(0, indd);
- if (s.equals(nov))
- return true;
- }
- return false;
- }
- public void add(String s) {
- lista.add(s);
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- RoutingHashJava x = new RoutingHashJava();
- Scanner br = new Scanner(System.in);
- int N = Integer.parseInt(br.nextLine());
- CBHT<Ruter, IPs> heshRuteri = x.new CBHT<Ruter, IPs>(200);
- for (int i = 0; i < N; i++) {
- Ruter r = x.new Ruter(br.nextLine());
- IPs ips = x.new IPs();
- String adresi = br.nextLine();
- if (adresi.indexOf(',') != -1) {
- String pomniza[] = adresi.split(",");
- for (int j = 0; j < pomniza.length; j++) {
- ips.add(pomniza[j]);
- }
- }
- else
- ips.add(adresi);
- heshRuteri.insert(r, ips);
- }
- int M = Integer.parseInt(br.nextLine());
- for (int k = 0; k < M; k++) {
- Ruter rr = x.new Ruter(br.nextLine());
- String adr = br.nextLine();
- if (heshRuteri.ima(rr, adr))
- System.out.println("postoi");
- else
- System.out.println("ne postoi");
- }
- br.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment