Filip_Markoski

[ADSx] - MostFrequentSubstring

Jan 11th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.Scanner;
  3.  
  4. class MapEntry<K extends Comparable<K>, E> implements Comparable<K> {
  5.  
  6.     K key;
  7.     E value;
  8.  
  9.     public MapEntry(K key, E val) {
  10.         this.key = key;
  11.         this.value = val;
  12.     }
  13.  
  14.     public int compareTo(K that) {
  15.         @SuppressWarnings("unchecked")
  16.         MapEntry<K, E> other = (MapEntry<K, E>) that;
  17.         return this.key.compareTo(other.key);
  18.     }
  19.  
  20.     public String toString() {
  21.         return "(" + key + "," + value + ")";
  22.     }
  23. }
  24.  
  25. class SLLNode<E> {
  26.     protected E element;
  27.     protected SLLNode<E> succ;
  28.  
  29.     public SLLNode(E elem, SLLNode<E> succ) {
  30.         this.element = elem;
  31.         this.succ = succ;
  32.     }
  33.  
  34.     @Override
  35.     public String toString() {
  36.         return element.toString();
  37.     }
  38. }
  39.  
  40. class CBHT<K extends Comparable<K>, E> {
  41.  
  42.     public SLLNode<MapEntry<K, E>>[] buckets;
  43.  
  44.     @SuppressWarnings("unchecked")
  45.     public CBHT(int m) {
  46.         buckets = (SLLNode<MapEntry<K, E>>[]) new SLLNode[m];
  47.     }
  48.  
  49.     private int hash(K key) {
  50.         return Math.abs(key.hashCode()) % buckets.length;
  51.     }
  52.  
  53.     public SLLNode<MapEntry<K, E>> search(K targetKey) {
  54.         int b = hash(targetKey);
  55.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  56.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  57.                 return curr;
  58.         }
  59.         return null;
  60.     }
  61.  
  62.     public E searchValue(K targetKey) {
  63.         int b = hash(targetKey);
  64.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  65.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  66.                 return curr.element.value;
  67.         }
  68.         return null;
  69.     }
  70.  
  71.     public void insert(K key, E val) {        // Insert the entry <key, val> into this CBHT.
  72.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  73.         int b = hash(key);
  74.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  75.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  76.                 curr.element = newEntry;
  77.                 return;
  78.             }
  79.         }
  80.         buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  81.     }
  82.  
  83.     public void delete(K key) {
  84.         int b = hash(key);
  85.         for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  86.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  87.                 if (pred == null)
  88.                     buckets[b] = curr.succ;
  89.                 else
  90.                     pred.succ = curr.succ;
  91.                 return;
  92.             }
  93.         }
  94.     }
  95.  
  96.     public String toString() {
  97.         StringBuilder sb = new StringBuilder();
  98.         for (int i = 0; i < buckets.length; i++) {
  99.             sb.append(i).append(":");
  100.             for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  101.                 sb.append(curr.element.key).append(" -> ");
  102.                 sb.append(curr.element.value).append(" ");
  103.             }
  104.             sb.append("\n");
  105.         }
  106.         return sb.toString();
  107.     }
  108.  
  109.     public static <K extends Comparable<K>, E extends Number> // E extends NUMBER
  110.     String mostFrequentSubstring(SLLNode<MapEntry<K, E>>[] buckets) {
  111.         String maxString = "";
  112.         int maxFrequency = Integer.MIN_VALUE;
  113.         for (int i = 0; i < buckets.length; i++) {
  114.             for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  115.                 E value = curr.element.value;
  116.                 String key = curr.element.key.toString();
  117.                 if (value.intValue() > maxFrequency) {
  118.                     maxFrequency = value.intValue();
  119.                     maxString = key;
  120.                 }
  121.                 // if it's smaller we don't care, because we're looking for MAX
  122.                 else if (value.intValue() == maxFrequency) {
  123.                     // if equal frequency return the longest
  124.                     if (key.length() > maxString.length()) {
  125.                         maxFrequency = value.intValue();
  126.                         maxString = key;
  127.                     } else if (key.length() == maxString.length()) {
  128.                         if (key.compareTo(maxString) < 0) { // same length but different letters
  129.                             maxFrequency = value.intValue();
  130.                             maxString = key; // get last longest string
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.         return maxString;
  137.     }
  138.  
  139. }
  140.  
  141. public class MostFrequentSubstring {
  142.     public static void main(String[] args) throws IOException {
  143.         CBHT<String, Integer> cbht = new CBHT<>(300);
  144.         Scanner scanner = new Scanner(System.in);
  145.  
  146.         String word = scanner.nextLine().trim();
  147.  
  148.         for (int i = 0; i < word.length(); i++) {
  149.             for (int j = i + 1; j <= word.length(); j++) {
  150.                 String subString = word.substring(i, j);
  151.                 Integer searched = cbht.searchValue(subString);
  152.                 if (searched == null) {
  153.                     cbht.insert(subString, 1);
  154.                 } else {
  155.                     cbht.insert(subString, searched + 1);
  156.                 }
  157.             }
  158.         }
  159.  
  160.         //System.out.println(cbht);
  161.  
  162.         System.out.println(CBHT.mostFrequentSubstring(cbht.buckets));
  163.  
  164.     }
  165. }
  166.  
  167.  
  168. /**
  169.  * For a given string, find the most frequent substring. If two substrings have the same frequency, then print the longer one, and if they are the same, print the one higher lexicographically.
  170.  * <p>
  171.  * Example: For the string "abc" substrings are "a", "b", "c", "ab", "bc", "abc". All have the same frequency, but "abc" is the longest.
  172.  * <p>
  173.  * Name of the class (Java): mostFrequentSubstring.
  174.  */
Add Comment
Please, Sign In to add comment