Advertisement
metalni

APS 2. Kolokvium Exercises Frekventen string [feat Acka]

Jan 19th, 2021 (edited)
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  6.  
  7.     K key;
  8.     E value;
  9.  
  10.     public MapEntry (K key, E val) {
  11.         this.key = key;
  12.         this.value = val;
  13.     }
  14.  
  15.     public int compareTo (K that) {
  16.         @SuppressWarnings("unchecked")
  17.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  18.         return this.key.compareTo(other.key);
  19.     }
  20.  
  21.     public String toString () {
  22.         return "(" + key + "," + value + ")";
  23.     }
  24. }
  25.  
  26. class SLLNode<E> {
  27.     protected E element;
  28.     protected SLLNode<E> succ;
  29.  
  30.     public SLLNode(E elem, SLLNode<E> succ) {
  31.         this.element = elem;
  32.         this.succ = succ;
  33.     }
  34.  
  35.     @Override
  36.     public String toString() {
  37.         return element.toString();
  38.     }
  39. }
  40.  
  41. class CBHT<K extends Comparable<K>, E> {
  42.  
  43.     private SLLNode<MapEntry<K,E>>[] buckets;
  44.  
  45.     @SuppressWarnings("unchecked")
  46.     public CBHT(int m) {
  47.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  48.     }
  49.  
  50.     private int hash(K key) {
  51.         return Math.abs(key.hashCode()) % buckets.length;
  52.     }
  53.  
  54.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  55.         int b = hash(targetKey);
  56.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  57.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  58.                 return curr;
  59.         }
  60.         return null;
  61.     }
  62.  
  63.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  64.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  65.         int b = hash(key);
  66.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  67.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  68.                 curr.element = newEntry;
  69.                 return;
  70.             }
  71.         }
  72.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  73.     }
  74.  
  75.     public void delete(K key) {
  76.         int b = hash(key);
  77.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  78.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  79.                 if (pred == null)
  80.                     buckets[b] = curr.succ;
  81.                 else
  82.                     pred.succ = curr.succ;
  83.                 return;
  84.             }
  85.         }
  86.     }
  87.  
  88.     public String toString() {
  89.         String temp = "";
  90.         for (int i = 0; i < buckets.length; i++) {
  91.             temp += i + ":";
  92.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  93.                 temp += curr.element.toString() + " ";
  94.             }
  95.             temp += "\n";
  96.         }
  97.         return temp;
  98.     }
  99.  
  100.     public SLLNode<MapEntry<K, E>>[] getBuckets() {
  101.         return buckets;
  102.     }
  103. }
  104.  
  105. public class MostFrequentSubstring {
  106.     public static void main (String[] args) throws IOException {
  107.         CBHT<String,Integer> tabela = new CBHT<String,Integer>(300);
  108.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  109.  
  110.         String word = br.readLine().trim();
  111.  
  112.         /*
  113.          *
  114.          * Vashiot kod tuka....
  115.          *
  116.          */
  117.  
  118.         //generiranje na site podstringovi
  119.         int b = word.length();
  120.         String [] words = new String[b*b];
  121.         int t = 0;
  122.         for(int i=0; i < b; i++){
  123.             for(int j=i; j < b; j++){
  124.                 words[t++] = word.substring(i, j+1);
  125.             }
  126.         }
  127.  
  128.         //proverka na cestota
  129.         for(int i=0; i < t; i++){
  130.             int cestota = 0;
  131.             for(int j=0; j < t; j++){
  132.                 if(words[i].equals(words[j]))
  133.                     cestota++;
  134.             }
  135.             //hashing
  136.             tabela.insert(words[i], cestota);
  137.         }
  138.  
  139.         //pecatenje na najcestiot string
  140.         int max = 0;
  141.         String maxString = "";
  142.         for(int i=0; i < tabela.getBuckets().length; i++){
  143.             SLLNode<MapEntry<String, Integer>> curr = tabela.getBuckets()[i];
  144.             while(curr != null){
  145.                 if(curr.element.value == max && curr.element.key.length() > maxString.length()){
  146.                     max = curr.element.value;
  147.                     maxString = curr.element.key;
  148.                 } else if(curr.element.value > max){
  149.                     max = curr.element.value;
  150.                     maxString = curr.element.key;
  151.                 }
  152.  
  153.                 curr = curr.succ;
  154.             }
  155.         }
  156.         System.out.println(maxString);
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement