Advertisement
ZlatniotOdBaba

Frequent string

May 25th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 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 max = Integer.MIN_VALUE;
  110. String maxString = null;
  111.  
  112. for (int i = 0; i < word.length(); ++i) {
  113. for (int j = i + 1; j <= word.length(); ++j) {
  114. String s = word.substring(i, j);
  115.  
  116. SLLNode<MapEntry<String, Integer>> search = tabela.search(s);
  117.  
  118. if (search == null)
  119. tabela.insert(s, 1);
  120. else {
  121. int thisLength = search.element.value + 1;
  122. tabela.insert(s, thisLength);
  123. if (thisLength > max) {
  124. maxString = s;
  125. max = thisLength;
  126. } else if (thisLength == max) {
  127. if (s.length() > maxString.length())
  128. maxString = s;
  129. else if (s.length() == maxString.length())
  130. if (s.compareTo(maxString) < 0)
  131. maxString = s;
  132. }
  133. }
  134. }
  135. }
  136. System.out.println(maxString);
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement