Advertisement
Latkoski

Фрекв

May 27th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. package hashJUNI;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.HashMap;
  6. public class MostFrequentSubstring {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  9.         HashMap<String, Integer> map = new HashMap<String,Integer>(200);
  10.         String red = br.readLine().trim();
  11.         int brojac=0;
  12.         for(int i=0;i<=red.length();i++){
  13.             for(int j=0;j+i<=red.length();j++){
  14.                 if(map.get(red.substring(j,i+j)) == null){
  15.                     map.put(red.substring(j,i+j), 1);
  16.                 }
  17.                 else{
  18.                     int result = map.get(red.substring(j,i+j));
  19.                     brojac = result + 1;
  20.                     map.put(red.substring(j,i+j),brojac);
  21.                 }
  22.             }
  23.         }
  24.         int maxi = 0;
  25.         int maxDolzina =0;
  26.         String najdolga = null;
  27.         for(int i=0;i<=red.length();i++){
  28.             for(int j=0;j+i<=red.length();j++){
  29.                 int result = map.get(red.substring(j,i+j));
  30.                 if(red.substring(j,j+i).equals("")){
  31.                     continue;
  32.                 }
  33.                 if(result > maxi){
  34.                     maxi = result;
  35.                     najdolga = red.substring(j,i+j);
  36.                 }
  37.                 if(result == maxi){
  38.                     if(red.substring(j,i+j).length() > maxDolzina){
  39.                         maxDolzina = red.substring(j,i+j).length();
  40.                         najdolga = red.substring(j,i+j);
  41.                     }
  42.                     else{
  43.                         if(red.substring(j,i+j).length() == maxDolzina){
  44.                             if(najdolga.compareTo(red.substring(j,i+j))>0){
  45.                                 najdolga = red.substring(j,i+j);
  46.                             }
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.         System.out.println(najdolga);
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement