Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- 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 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.
- String keyString = (String)key;
- return ((100 *keyString.charAt(0)+ keyString.charAt(1)) % 9091) % 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.equals(((MapEntry<K, E>) curr.element).key))
- return curr;
- }
- return null;
- }
- public SLLNode<MapEntry<K,E>> findFirstInTheList(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.equals(((MapEntry<K, E>) curr.element).key))
- return buckets[b];
- }
- 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(((MapEntry<K, E>) curr.element).key)) {
- // Make newEntry replace the existing entry ...
- curr.element = newEntry;
- return;
- }
- }
- // Insert newEntry at the front of the 1WLL in bucket b ...
- if(buckets[b]==null)
- {
- buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
- }
- else
- {
- SLLNode<MapEntry<K,E>> curr = buckets[b];
- while(curr.succ!=null)
- {
- curr = curr.succ;
- }
- curr.succ = new SLLNode<MapEntry<K,E>>(newEntry, null);
- }
- }
- 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 class Main
- {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int N = in.nextInt();
- CBHT<String, Integer> maski = new CBHT(2*N);
- CBHT<String, Integer> zenski = new CBHT(2*N);
- for(int i=0;i<N;i++)
- {
- String ime = in.next();
- String pol = in.next();
- if(pol.equals("M"))
- {
- SLLNode<MapEntry<String,Integer>> curr = maski.search(ime);
- if(curr==null)
- {
- maski.insert(ime,1);
- }
- else
- {
- int zastapenost = curr.element.value + 1;
- maski.insert(ime,zastapenost);
- }
- }
- else
- {
- SLLNode<MapEntry<String,Integer>> curr = zenski.search(ime);
- if(curr==null)
- {
- zenski.insert(ime,1);
- }
- else
- {
- int zastapenost = curr.element.value + 1;
- zenski.insert(ime,zastapenost);
- }
- }
- }
- String pol = in.next();
- CBHT<String,Integer> table;
- if(pol.equals("M"))
- table = maski;
- else
- table = zenski;
- while(true)
- {
- String ime = in.next();
- if(ime.equalsIgnoreCase("KRAJ"))
- break;
- SLLNode<MapEntry<String,Integer>> curr = table.findFirstInTheList(ime);
- if(curr!=null)
- {
- while(curr.succ!=null)
- {
- System.out.print(curr.element.key + ", ");
- curr = curr.succ;
- }
- System.out.println(curr.element.key);
- SLLNode<MapEntry<String,Integer>> curr2 = table.search(ime);
- System.out.println(pol + " " + ime + " " + curr2.element.value);
- }
- else
- {
- System.out.println("Nema takvo ime");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement