Advertisement
elena_gjorgjioska

HashFrekventenString

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