Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 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 pom;
  8.         String w;
  9.         /*
  10.         *
  11.         * Vashiot kod tuka....
  12.         *
  13.         */
  14.         for(int i=1; i<=word.length(); i++) {
  15.             for(int j=0; j<=word.length()-i; j++) {
  16.  
  17.                 pom=word.substring(j, j+i);
  18.  
  19.                 if(tabela.search(pom)==null) {
  20.                     tabela.insert(pom, 1);
  21.  
  22.                 }
  23.                 else
  24.                 {
  25.                 int val=tabela.search(pom).element.value;
  26.                 val++;
  27.                 tabela.insert(pom, val);
  28.                
  29.                 }
  30.  
  31.             }
  32.  
  33.  
  34.         }
  35.        
  36.         //System.out.println(tabela);
  37.         int max=0;
  38.         String maxs="";
  39.         for(int i=1; i<=word.length(); i++) {
  40.             for(int j=0; j<=word.length()-i; j++) {
  41.                 w=word.substring(j, j+i);
  42.                 int value = tabela.search(w).element.value;
  43.                 if(value>max)
  44.                 {
  45.                     max=value;
  46.                     maxs=tabela.search(w).element.key;
  47.                 }
  48.                 else if (value == max) {
  49.                     if (w.length() > maxs.length())
  50.                     {
  51.                     maxs=w;
  52.                     }
  53.                     else if (w.length() == maxs.length())
  54.                     {
  55.                         if (w.compareTo(maxs) < 0) {
  56.                             maxs = w;
  57.                         }
  58.                     }
  59.                 }
  60.                
  61.             }}
  62.  
  63.         System.out.println(maxs);
  64.  
  65.  
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement