Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.55 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
  64.         // CBHT.
  65.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  66.         int b = hash(key);
  67.         for (SLLNode<MapEntry<K, E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  68.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  69.                 curr.element = newEntry;
  70.                 return;
  71.             }
  72.         }
  73.         buckets[b] = new SLLNode<MapEntry<K, E>>(newEntry, buckets[b]);
  74.     }
  75.  
  76.     public void delete(K key) {
  77.         int b = hash(key);
  78.         for (SLLNode<MapEntry<K, E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  79.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  80.                 if (pred == null)
  81.                     buckets[b] = curr.succ;
  82.                 else
  83.                     pred.succ = curr.succ;
  84.                 return;
  85.             }
  86.         }
  87.     }
  88.  
  89.     public String toString() {
  90.         String temp = "";
  91.         for (int i = 0; i < buckets.length; i++) {
  92.             temp += i + ":";
  93.             for (SLLNode<MapEntry<K, E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  94.                 temp += curr.element.toString() + " ";
  95.             }
  96.             temp += "\n";
  97.         }
  98.         return temp;
  99.     }
  100.  
  101. }
  102.  
  103. public class MostFrequentSubstring {
  104.     public static void main(String[] args) throws IOException {
  105.         CBHT<String, Integer> tabela = new CBHT<String, Integer>(300);
  106.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  107.  
  108.         String word = br.readLine().trim();
  109.         int k = 0;
  110.         for (int i = 0; i < word.length(); i++) {
  111.             for (int j = i; j <= word.length(); j++) {
  112.                 String current = word.substring(i, j);
  113.  
  114.                 if (!current.equals("")) {
  115.                     SLLNode<MapEntry<String, Integer>> found = tabela
  116.                             .search(current);
  117.  
  118.                     if (found != null) {
  119.                         tabela.insert(current, found.element.value + 1);
  120.                     } else {
  121.                         tabela.insert(current, 1);
  122.                     }
  123.                     k++;
  124.                 }
  125.             }
  126.         }
  127.  
  128.         String mostFrequent = "";
  129.         int maxVal = 1;
  130.  
  131.         for (int i = 0; i < word.length(); i++) {
  132.             for (int j = i; j <= word.length(); j++) {
  133.                 String current = word.substring(i, j);
  134.                 if (!current.equals("")) {
  135.                     SLLNode<MapEntry<String, Integer>> found = tabela
  136.                             .search(current);
  137.  
  138.                     if (found.element.value > maxVal) {
  139.                         maxVal = found.element.value;
  140.                         mostFrequent = found.element.key;
  141.                     } else if (found.element.value == maxVal) {
  142.  
  143.                         int lenFound = found.element.key.length();
  144.                         int lenMax = mostFrequent.length();
  145.  
  146.                         if (lenFound > lenMax) {
  147.                             maxVal = found.element.value;
  148.                             mostFrequent = found.element.key;
  149.                         }
  150.  
  151.                         if( lenFound == lenMax ) {
  152.                             int asciiFound = 0;
  153.                             int asciiMax = 0;
  154.  
  155.                             for(int z = 0; z < found.element.key.length(); z++) {
  156.                                 asciiFound += (int)found.element.key.charAt(z);
  157.                             }
  158.                             for(int z = 0; z < mostFrequent.length(); z++) {
  159.                                 asciiMax += (int)mostFrequent.charAt(z);
  160.                             }
  161.  
  162.                             if( asciiFound < asciiMax ) {
  163.                                 mostFrequent = found.element.key;
  164.                                 maxVal = found.element.value;
  165.                             }
  166.                         }
  167.  
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.  
  173.         System.out.println(mostFrequent);
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement