Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- 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();
- }
- }
- 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) {
- // Compare this map entry to that map entry.
- @SuppressWarnings("unchecked")
- MapEntry<K,E> other = (MapEntry<K,E>) that;
- return this.key.compareTo(other.key);
- }
- public String toString () {
- return "<" + key + "," + value + ">";
- }
- }
- class OBHT<K extends Comparable<K>,E> {
- // An object of class OBHT is an open-bucket hash table, containing entries
- // of class MapEntry.
- public MapEntry<K,E>[] buckets;
- // buckets[b] is null if bucket b has never been occupied.
- // buckets[b] is former if bucket b is formerly-occupied
- // by an entry that has since been deleted (and not yet replaced).
- static final int NONE = -1; // ... distinct from any bucket index.
- private static final MapEntry former = new MapEntry(null, null);
- // This guarantees that, for any genuine entry e,
- // e.key.equals(former.key) returns false.
- private int occupancy = 0;
- // ... number of occupied or formerly-occupied buckets in this OBHT.
- @SuppressWarnings("unchecked")
- public OBHT (int m) {
- // Construct an empty OBHT with m buckets.
- buckets = (MapEntry<K,E>[]) new MapEntry[m];
- }
- private int hash (K key) {
- // Translate key to an index of the array buckets.
- return Math.abs(key.hashCode()) % buckets.length;
- }
- public int search (K targetKey) {
- // Find which if any bucket of this OBHT is occupied by an entry whose key
- // is equal to targetKey. Return the index of that bucket.
- int b = hash(targetKey); int n_search=0;
- for (;;) {
- MapEntry<K,E> oldEntry = buckets[b];
- if (oldEntry == null)
- return NONE;
- else if (targetKey.equals(oldEntry.key))
- return b;
- else
- {
- b = (b + 1) % buckets.length;
- n_search++;
- if(n_search==buckets.length)
- return NONE;
- }
- }
- }
- public void insert (K key, E val) {
- // Insert the entry <key, val> into this OBHT.
- MapEntry<K,E> newEntry = new MapEntry<K,E>(key, val);
- int b = hash(key); int n_search=0;
- for (;;) {
- MapEntry<K,E> oldEntry = buckets[b];
- if (oldEntry == null) {
- if (++occupancy == buckets.length) {
- System.out.println("Hash tabelata e polna!!!");
- }
- buckets[b] = newEntry;
- return;
- }
- else if (oldEntry == former
- || key.equals(oldEntry.key)) {
- buckets[b] = newEntry;
- return;
- }
- else
- {
- b = (b + 1) % buckets.length;
- n_search++;
- if(n_search==buckets.length)
- return;
- }
- }
- }
- @SuppressWarnings("unchecked")
- public void delete (K key) {
- // Delete the entry (if any) whose key is equal to key from this OBHT.
- int b = hash(key); int n_search=0;
- for (;;) {
- MapEntry<K,E> oldEntry = buckets[b];
- if (oldEntry == null)
- return;
- else if (key.equals(oldEntry.key)) {
- buckets[b] = former;//(MapEntry<K,E>)former;
- return;
- }
- else{
- b = (b + 1) % buckets.length;
- n_search++;
- if(n_search==buckets.length)
- return;
- }
- }
- }
- public String toString () {
- String temp = "";
- for (int i = 0; i < buckets.length; i++) {
- temp += i + ":";
- if (buckets[i] == null)
- temp += "\n";
- else if (buckets[i] == former)
- temp += "former\n";
- else
- temp += buckets[i] + "\n";
- }
- return temp;
- }
- public OBHT<K,E> clone () {
- OBHT<K,E> copy = new OBHT<K,E>(buckets.length);
- for (int i = 0; i < buckets.length; i++) {
- MapEntry<K,E> e = buckets[i];
- if (e != null && e != former)
- copy.buckets[i] = new MapEntry<K,E>(e.key, e.value);
- else
- copy.buckets[i] = e;
- }
- return copy;
- }
- }
- class Vraboten implements Comparable<Vraboten>
- {
- String ime;
- String prezime;
- int den, mesec, godina;
- public Vraboten(String ime, String prezime, int den, int mesec, int godina) {
- super();
- this.ime = ime;
- this.prezime = prezime;
- this.den = den;
- this.mesec = mesec;
- this.godina = godina;
- }
- @Override
- public int compareTo(Vraboten arg0) {
- // TODO Auto-generated method stub
- if(ime.compareTo(arg0.ime)==0)
- return prezime.compareTo(arg0.prezime);
- return ime.compareTo(arg0.ime);
- }
- /*public int compareTo(Vraboten arg0) {
- // TODO Auto-generated method stub
- return arg0.godina-godina;
- }*/
- }
- public class HashRodendeni {
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String s, ime, prezime, datum;
- String[] pom;
- String[] pomoshna;
- int i, n, den, mesec, godina, h;
- s = br.readLine();
- n = Integer.parseInt(s);
- OBHT<String,ArrayList<Vraboten>> tabela = new OBHT<String,ArrayList<Vraboten>>(n+1);
- ArrayList<Vraboten> v;
- for(i=0; i<n; i++)
- {
- s = br.readLine();
- pom = s.split(" ");
- ime = pom[0];
- prezime = pom[1];
- datum = pom[2];
- pomoshna = datum.split("/");
- den = Integer.parseInt(pomoshna[0]);
- mesec = Integer.parseInt(pomoshna[1]);
- godina = Integer.parseInt(pomoshna[2]);
- Vraboten k = new Vraboten(ime,prezime,den,mesec,godina);
- String denMesec = pomoshna[0]+"/"+pomoshna[1];
- h = tabela.search(denMesec);
- //System.out.println(denMesec);
- if(h == -1)
- {
- v = new ArrayList<Vraboten>();
- v.add(k);
- tabela.insert(denMesec, v);
- }
- else
- {
- v = tabela.buckets[h].value;
- v.add(k);
- tabela.insert(denMesec, v);
- }
- }
- s = br.readLine();
- pomoshna = s.split("/");
- String denMesec = pomoshna[0]+ "/" + pomoshna[1];
- int god = Integer.parseInt(pomoshna[2]);
- //System.out.println(denMesec);
- h = tabela.search(denMesec);
- if(h == -1)
- System.out.println("Nema");
- else
- {
- v = tabela.buckets[h].value;
- //v.sort(null);
- Collections.sort(v);
- for(i=0; i<v.size(); i++)
- {
- int f = god - v.get(i).godina;
- System.out.println(v.get(i).ime +" " + v.get(i).prezime+ " " + f);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement