Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MostFrequentSubstring {
- public static void main (String[] args) throws IOException {
- CBHT<String,Integer> tabela = new CBHT<String,Integer>(300);
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String word = br.readLine().trim();
- String sub;
- int max = 0; String maxSub = "/";
- for (int c = 0; c < word.length(); c++) {
- for (int i = 1; i <= word.length() - c; i++) {
- sub = word.substring(c, c + i);
- //System.out.println(sub);
- int idx = 0, f = 0, res = -1;
- while (true) // (true)
- {
- idx = word.indexOf(sub, idx);
- if (idx == -1)
- break;
- //System.out.println(sub + " na idx: " + idx);
- f++;
- idx += sub.length();
- }
- int lexi = sub.compareTo(maxSub);
- if (f > max
- || (f == max&&(maxSub.length() < sub.length()))
- || (lexi < 0 && (maxSub.length() < sub.length())))
- {
- max = f;
- maxSub = sub;
- }
- //System.out.println(sub + ": " + f);
- tabela.insert(sub, f);
- }
- }
- System.out.println(maxSub);
- //System.out.println(tabela);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement