Advertisement
Guest User

Untitled

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