Advertisement
HristijanBosheski

Фреквентен стринг [АПС Вежби за втор колоквиум]

Jan 19th, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class CBHT<K extends Comparable<K>, E> {
  6.  
  7.     // An object of class CBHT is a closed-bucket hash table, containing
  8.     // entries of class MapEntry.
  9.     private SLLNode<MapEntry<K,E>>[] buckets;
  10.  
  11.     @SuppressWarnings("unchecked")
  12.     public CBHT(int m) {
  13.         // Construct an empty CBHT with m buckets.
  14.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  15.     }
  16.  
  17.     private int hash(K key) {
  18.         // Translate key to an index of the array buckets.
  19.         return Math.abs(key.hashCode()) % buckets.length;
  20.     }
  21.  
  22.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  23.         // Find which if any node of this CBHT contains an entry whose key is
  24.         // equal
  25.         // to targetKey. Return a link to that node (or null if there is none).
  26.         int b = hash(targetKey);
  27.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  28.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  29.                 return curr;
  30.         }
  31.         return null;
  32.     }
  33.  
  34.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  35.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  36.         int b = hash(key);
  37.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  38.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  39.                 // Make newEntry replace the existing entry ...
  40.                 curr.element = newEntry;
  41.                 return;
  42.             }
  43.         }
  44.         // Insert newEntry at the front of the 1WLL in bucket b ...
  45.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  46.     }
  47.  
  48.     public void delete(K key) {
  49.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  50.         int b = hash(key);
  51.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  52.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  53.                 if (pred == null)
  54.                     buckets[b] = curr.succ;
  55.                 else
  56.                     pred.succ = curr.succ;
  57.                 return;
  58.             }
  59.         }
  60.     }
  61.  
  62.     public String toString() {
  63.         String temp = "";
  64.         for (int i = 0; i < buckets.length; i++) {
  65.             temp += i + ":";
  66.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  67.                 temp += curr.element.toString() + " ";
  68.             }
  69.             temp += "\n";
  70.         }
  71.         return temp;
  72.     }
  73.  
  74. }
  75.  
  76. class SLLNode<E> {
  77.     protected E element;
  78.     protected SLLNode<E> succ;
  79.  
  80.     public SLLNode(E elem, SLLNode<E> succ) {
  81.         this.element = elem;
  82.         this.succ = succ;
  83.     }
  84.  
  85.     @Override
  86.     public String toString() {
  87.         return element.toString();
  88.     }
  89. }
  90.  
  91. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  92.  
  93.     // Each MapEntry object is a pair consisting of a key (a Comparable
  94.     // object) and a value (an arbitrary object).
  95.     K key;
  96.     E value;
  97.  
  98.     public MapEntry (K key, E val) {
  99.         this.key = key;
  100.         this.value = val;
  101.     }
  102.    
  103.     public int compareTo (K that) {
  104.     // Compare this map entry to that map entry.
  105.         @SuppressWarnings("unchecked")
  106.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  107.         return this.key.compareTo(other.key);
  108.     }
  109.  
  110.     public String toString () {
  111.         return "<" + key + "," + value + ">";
  112.     }
  113. }
  114.  
  115. public class MostFrequentSubstring {
  116.  
  117.     public static void main(String[] args) throws IOException {
  118.  
  119.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  120.         String expr = br.readLine();
  121.        
  122.         CBHT<String, Integer> h = new CBHT<>(expr.length()*expr.length());
  123.         for(int i=0; i<expr.length(); i++){
  124.             for(int j=i+1; j<=expr.length(); j++){
  125.                 if(h.search(expr.substring(i, j)) != null){
  126.                     h.insert(expr.substring(i, j), h.search(expr.substring(i, j)).element.value+1);
  127.                 }
  128.                 else{
  129.                     h.insert(expr.substring(i, j), 1);
  130.                 }
  131.             }
  132.         }
  133.  
  134.         int maxVal = 0;
  135.         String maxKey = "";
  136.        
  137.         for(int i=0; i<expr.length(); i++){
  138.             for(int j=i+1; j<=expr.length(); j++){
  139.                 if(h.search(expr.substring(i, j)).element.value > maxVal){
  140.                     maxVal = h.search(expr.substring(i, j)).element.value;
  141.                     maxKey = h.search(expr.substring(i, j)).element.key;
  142.                 }
  143.                 else if(h.search(expr.substring(i, j)).element.value == maxVal){
  144.                     if(h.search(expr.substring(i, j)).element.key.length() > maxKey.length()){
  145.                         maxVal = h.search(expr.substring(i, j)).element.value;
  146.                         maxKey = h.search(expr.substring(i, j)).element.key;
  147.                     }
  148.                     else if(h.search(expr.substring(i, j)).element.key.length() == maxKey.length()){
  149.                         if(h.search(expr.substring(i, j)).element.key.compareTo(maxKey) < 0){
  150.                             maxVal = h.search(expr.substring(i, j)).element.value;
  151.                             maxKey = h.search(expr.substring(i, j)).element.key;
  152.                         }
  153.                     }
  154.                 }
  155.             }
  156.         }
  157.        
  158.         System.out.println(maxKey);
  159.        
  160.         br.close();
  161.  
  162.     }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement