Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. public class MostFrequentSubstring {
  2.     public static void main (String[] args) throws IOException {
  3.         CBHT<String,Integer> tabela = new CBHT<String,Integer>(300);
  4.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  5.        
  6.         String word = br.readLine().trim();
  7.         String sub;
  8.         int max = 0; String maxSub = "/";
  9.         for (int c = 0; c < word.length(); c++) {
  10.             for (int i = 1; i <= word.length() - c; i++) {
  11.                 sub = word.substring(c, c + i);
  12.                 //System.out.println(sub);
  13.                 int idx = 0, f = 0, res = -1;
  14.                 while (true) // (true)
  15.                 {
  16.                     idx = word.indexOf(sub, idx);
  17.                     if (idx == -1)
  18.                         break;
  19.                     //System.out.println(sub + " na idx: " + idx);
  20.                     f++;
  21.                     idx += sub.length();
  22.                 }
  23.                 int lexi = sub.compareTo(maxSub);
  24.                 if (f > max
  25.                         || (f == max&&(maxSub.length() < sub.length()))
  26.                         || (lexi < 0 && (maxSub.length() < sub.length())))
  27.                 {
  28.                     max = f;
  29.                     maxSub = sub;
  30.                 }
  31.                 //System.out.println(sub + ": " + f);
  32.                 tabela.insert(sub, f);
  33.             }
  34.         }
  35.        
  36.         System.out.println(maxSub);
  37.         //System.out.println(tabela);
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement