Advertisement
HeatPulse

frekventen

Aug 26th, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. package com.company;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  7.  
  8. K key;
  9. E value;
  10.  
  11. public MapEntry (K key, E val) {
  12. this.key = key;
  13. this.value = val;
  14. }
  15.  
  16. public int compareTo (K that) {
  17. @SuppressWarnings("unchecked")
  18. MapEntry<K,E> other = (MapEntry<K,E>) that;
  19. return this.key.compareTo(other.key);
  20. }
  21.  
  22. public String toString () {
  23. return "(" + key + "," + value + ")";
  24. }
  25. }
  26.  
  27. class SLLNode<E> {
  28. protected E element;
  29. protected SLLNode<E> succ;
  30.  
  31. public SLLNode(E elem, SLLNode<E> succ) {
  32. this.element = elem;
  33. this.succ = succ;
  34. }
  35.  
  36. @Override
  37. public String toString() {
  38. return element.toString();
  39. }
  40. }
  41.  
  42. class CBHT<K extends Comparable<K>, E> {
  43.  
  44. private SLLNode<MapEntry<K,E>>[] buckets;
  45.  
  46. @SuppressWarnings("unchecked")
  47. public CBHT(int m) {
  48. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  49. }
  50.  
  51. private int hash(K key) {
  52. return Math.abs(key.hashCode()) % buckets.length;
  53. }
  54.  
  55. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  56. int b = hash(targetKey);
  57. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  58. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  59. return curr;
  60. }
  61. return null;
  62. }
  63.  
  64. public void insert(K key, E val) { // Insert the entry <key, val> into this 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.  
  105. public static void main (String[] args) throws IOException {
  106. CBHT<String,Integer> tabela = new CBHT<String,Integer>(300);
  107. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  108.  
  109. String word = br.readLine().trim();
  110.  
  111. /*
  112. *
  113. * Vashiot kod tuka....
  114. *
  115. */
  116. int max=0;
  117. String maxString = "";
  118. for( int i=1; i <= word.length();i++){
  119. for(int j =0; j <= word.length()-i; j++){
  120. String podString = word.substring(j,j+i);
  121. //System.out.println(word.substring(j,j+i));
  122. if( tabela.search(podString) != null){
  123. tabela.insert(podString,tabela.search(podString).element.value+1);
  124. if( max == tabela.search(podString).element.value+1) {
  125. if(podString.length() > maxString.length()) maxString = podString;
  126. else if(podString.length() == maxString.length()){
  127. if(podString.compareTo(maxString) == -1){
  128. maxString = podString;
  129. }
  130. }
  131. max = tabela.search(podString).element.value + 1;
  132.  
  133. }
  134. if( max < tabela.search(podString).element.value+1){
  135. max = tabela.search(podString).element.value + 1;
  136. maxString = podString;
  137. }
  138.  
  139. }
  140. else {
  141. tabela.insert(podString,1);
  142. if( max <= 1) {
  143. max = 1;
  144. maxString = podString;
  145. }
  146.  
  147. }
  148.  
  149.  
  150.  
  151. }
  152. }
  153. System.out.println(maxString);
  154. //System.out.println(tabela);
  155.  
  156.  
  157.  
  158. }
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement