Advertisement
ivana_andreevska

Zavod za statistika

Feb 8th, 2022
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.63 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  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.  
  18.  
  19. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  20.  
  21.     // Each MapEntry object is a pair consisting of a key (a Comparable
  22.     // object) and a value (an arbitrary object).
  23.     K key;
  24.     E value;
  25.  
  26.     public MapEntry (K key, E val) {
  27.         this.key = key;
  28.         this.value = val;
  29.     }
  30.    
  31.     public int compareTo (K that) {
  32.     // Compare this map entry to that map entry.
  33.         @SuppressWarnings("unchecked")
  34.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  35.         return this.key.compareTo(other.key);
  36.     }
  37.  
  38.     public String toString () {
  39.         return "<" + key + "," + value + ">";
  40.     }
  41. }
  42.  
  43. class CBHT<K extends Comparable<K>, E> {
  44.  
  45.     // An object of class CBHT is a closed-bucket hash table, containing
  46.     // entries of class MapEntry.
  47.     private SLLNode<MapEntry<K,E>>[] buckets;
  48.  
  49.     @SuppressWarnings("unchecked")
  50.     public CBHT(int m) {
  51.         // Construct an empty CBHT with m buckets.
  52.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  53.     }
  54.  
  55.     private int hash(K key) {
  56.         // Translate key to an index of the array buckets.
  57.         String keyString = (String)key;
  58.         return ((100 *keyString.charAt(0)+ keyString.charAt(1)) % 9091) % buckets.length;
  59.     }
  60.  
  61.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  62.         // Find which if any node of this CBHT contains an entry whose key is
  63.         // equal
  64.         // to targetKey. Return a link to that node (or null if there is none).
  65.         int b = hash(targetKey);
  66.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  67.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  68.                 return curr;
  69.         }
  70.         return null;
  71.     }
  72.     public SLLNode<MapEntry<K,E>> findFirstInTheList(K targetKey) {
  73.         // Find which if any node of this CBHT contains an entry whose key is
  74.         // equal
  75.         // to targetKey. Return a link to that node (or null if there is none).
  76.         int b = hash(targetKey);
  77.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  78.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  79.                 return buckets[b];
  80.         }
  81.         return null;
  82.     }
  83.  
  84.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  85.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  86.         int b = hash(key);
  87.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  88.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  89.                 // Make newEntry replace the existing entry ...
  90.                 curr.element = newEntry;
  91.                 return;
  92.             }
  93.         }
  94.         // Insert newEntry at the front of the 1WLL in bucket b ...
  95.         if(buckets[b]==null)
  96.         {
  97.             buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  98.         }
  99.         else
  100.         {
  101.             SLLNode<MapEntry<K,E>> curr = buckets[b];
  102.             while(curr.succ!=null)
  103.             {
  104.                 curr = curr.succ;
  105.             }
  106.             curr.succ = new SLLNode<MapEntry<K,E>>(newEntry, null);
  107.         }
  108.     }
  109.  
  110.     public void delete(K key) {
  111.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  112.         int b = hash(key);
  113.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  114.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  115.                 if (pred == null)
  116.                     buckets[b] = curr.succ;
  117.                 else
  118.                     pred.succ = curr.succ;
  119.                 return;
  120.             }
  121.         }
  122.     }
  123.  
  124.     public String toString() {
  125.         String temp = "";
  126.         for (int i = 0; i < buckets.length; i++) {
  127.             temp += i + ":";
  128.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  129.                 temp += curr.element.toString() + " ";
  130.             }
  131.             temp += "\n";
  132.         }
  133.         return temp;
  134.     }
  135.  
  136. }
  137.  
  138.  
  139. public class Main
  140. {
  141.     public static void main(String[] args) {
  142.         Scanner in = new Scanner(System.in);
  143.         int N = in.nextInt();
  144.         CBHT<String, Integer> maski = new CBHT(2*N);
  145.         CBHT<String, Integer> zenski = new CBHT(2*N);
  146.         for(int i=0;i<N;i++)
  147.         {
  148.             String ime = in.next();
  149.             String pol = in.next();
  150.             if(pol.equals("M"))
  151.             {
  152.                 SLLNode<MapEntry<String,Integer>> curr = maski.search(ime);
  153.                 if(curr==null)
  154.                 {
  155.                     maski.insert(ime,1);
  156.                 }
  157.                 else
  158.                 {
  159.                     int zastapenost = curr.element.value + 1;
  160.                     maski.insert(ime,zastapenost);
  161.                 }
  162.                
  163.             }
  164.             else
  165.             {
  166.                  SLLNode<MapEntry<String,Integer>> curr = zenski.search(ime);
  167.                 if(curr==null)
  168.                 {
  169.                     zenski.insert(ime,1);
  170.                 }
  171.                 else
  172.                 {
  173.                     int zastapenost = curr.element.value + 1;
  174.                     zenski.insert(ime,zastapenost);
  175.                 }
  176.             }
  177.         }
  178.        
  179.         String pol = in.next();
  180.         CBHT<String,Integer> table;
  181.         if(pol.equals("M"))
  182.             table = maski;
  183.         else
  184.             table = zenski;
  185.         while(true)
  186.         {
  187.             String ime = in.next();
  188.             if(ime.equalsIgnoreCase("KRAJ"))
  189.                 break;
  190.             SLLNode<MapEntry<String,Integer>> curr = table.findFirstInTheList(ime);
  191.            
  192.            
  193.             if(curr!=null)
  194.             {
  195.                 while(curr.succ!=null)
  196.                 {
  197.                     System.out.print(curr.element.key + ", ");
  198.                     curr = curr.succ;
  199.                 }
  200.                 System.out.println(curr.element.key);
  201.                 SLLNode<MapEntry<String,Integer>> curr2 = table.search(ime);
  202.                 System.out.println(pol + " " + ime + " " + curr2.element.value);
  203.             }
  204.             else
  205.             {
  206.                 System.out.println("Nema takvo ime");
  207.             }
  208.         }
  209.        
  210.        
  211.     }
  212. }
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement