import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.HashMap;
public class Main {
private static boolean isLetter(char c) {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
}
private static void increaseFrequency
(Map
<String, Integer
> words,
String currentWord
) { if (words.containsKey(currentWord)) {
words.put(currentWord, words.get(currentWord) + 1);
} else {
words.put(currentWord, 1);
}
}
private static boolean isMoreFrequent(StringBuilder currentWord, StringBuilder recurrentWord, int currentOcurance, int maxOcurance) {
return (currentOcurance > maxOcurance) || (currentOcurance == maxOcurance && (recurrentWord == null || recurrentWord.compareTo(currentWord) > 0));
}
private static void updateRecurrentWord(StringBuilder recurrentWord, StringBuilder currentWord, int[] maxOcurance, int currentOcurance) {
if (isMoreFrequent(currentWord, recurrentWord, currentOcurance, maxOcurance[0])) {
recurrentWord.replace(0, recurrentWord.length(), currentWord.toString());
maxOcurance[0] = currentOcurance;
}
}
Map
<String, Integer
> words
= new HashMap
<>(); StringBuilder recurrentWord = null;
int[] maxOcurance = new int[1];
try {
while (reader.ready()) {
String currentLine
= reader.
readLine(); StringBuilder currentWord = new StringBuilder();
boolean wasLetter = false;
int length = currentLine.length();
for (int i = 0; i < length; ++i) {
if (isLetter(currentLine.charAt(i))) {
currentWord.append(currentLine.charAt(i));
wasLetter = true;
} else if (wasLetter) {
increaseFrequency(words, currentWord.toString());
updateRecurrentWord(recurrentWord, currentWord, maxOcurance, words.get(currentWord.toString()));
currentWord = new StringBuilder();
wasLetter = false;
}
}
if (currentWord.length() > 0) {
increaseFrequency(words, currentWord.toString());
updateRecurrentWord(recurrentWord, currentWord, maxOcurance, words.get(currentWord.toString()));
}
}
try {
return recurrentWord.toString();
return "Nu ai introdus nici-un text.";
}
return "Eroare la citirea textului.";
}
}
public static void main
(String[] args
) { System.
out.
println(frequestWord
(reader
)); }
}
aW1wb3J0IGphdmEuaW8uQnVmZmVyZWRSZWFkZXI7CmltcG9ydCBqYXZhLmlvLklPRXhjZXB0aW9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ZW50V29yZChTdHJpbmdCdWlsZGVyIHJlY3VycmVudFdvcmQsIFN0cmluZ0J1aWxkZXIgY3VycmVudFdvcmQsIGludFtdIG1heE9jdXJhbmNlLCBpbnQgY3VycmVudE9jdXJhbmNlKSB7CiAgICAgICAgaWYgKGlzTW9yZUZyZXF1ZW50KGN1cnJlbnRXb3JkLCByZWN1cnJlbnRXb3JkLCBjdXJyZW50T2N1cmFuY2UsIG1heE9jdXJhbmNlWzBdKSkgewogICAgICAgICAgICByZWN1cnJlbnRXb3JkLnJlcGxhY2UoMCwgcmVjdXJyZW50V29yZC5sZW5ndGgoKSwgY3VycmVudFdvcmQudG9TdHJpbmcoKSk7CiAgICAgICAgICAgIG1heE9jdXJhbmNlWzBdID0gY3VycmVudE9jdXJhbmNlOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIFN0cmluZyBmcmVxdWVzdFdvcmQoQnVmZmVyZWRSZWFkZXIgcmVhZGVyKSB7CiAgICAgICAgTWFwPFN0cmluZywgSW50ZWdlcj4gd29yZHMgPSBuZXcgSGFzaE1hcDw+KCk7CiAgICAgICAgU3RyaW5nQnVpbGRlciByZWN1cnJlbnRXb3JkID0gbnVsbDsKICAgICAgICBpbnRbXSBtYXhPY3VyYW5jZSA9IG5ldyBpbnRbMV07CiAgICAgICAgdHJ5IHsKICAgICAgICAgICAgd2hpbGUgKHJlYWRlci5yZWFkeSgpKSB7CiAgICAgICAgICAgICAgICBTdHJpbmcgY3VycmVudExpbmUgPSByZWFkZXIucmVhZExpbmUoKTsKICAgICAgICAgICAgICAgIFN0cmluZ0J1aWxkZXIgY3VycmVudFdvcmQgPSBuZXcgU3RyaW5nQnVpbGRlcigpOwogICAgICAgICAgICAgICAgYm9vbGVhbiB3YXNMZXR0ZXIgPSBmYWxzZTsKICAgICAgICAgICAgICAgIGludCBsZW5ndGggPSBjdXJyZW50TGluZS5sZW5ndGgoKTsKICAgICAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbGVuZ3RoOyArK2kpIHsKICAgICAgICAgICAgICAgICAgICBpZiAoaXNMZXR0ZXIoY3VycmVudExpbmUuY2hhckF0KGkpKSkgewogICAgICAgICAgICAgICAgICAgICAgICBjdXJyZW50V29yZC5hcHBlbmQoY3VycmVudExpbmUuY2hhckF0KGkpKTsKICAgICAgICAgICAgICAgICAgICAgICAgd2FzTGV0dGVyID0gdHJ1ZTsKICAgICAgICAgICAgICAgICAgICB9IGVsc2UgaWYgKHdhc0xldHRlcikgewogICAgICAgICAgICAgICAgICAgICAgICBpbmNyZWFzZUZyZXF1ZW5jeSh3b3JkcywgY3VycmVudFdvcmQudG9TdHJpbmcoKSk7CiAgICAgICAgICAgICAgICAgICAgICAgIHVwZGF0ZVJlY3VycmVudFdvcmQocmVjdXJyZW50V29yZCwgY3VycmVudFdvcmQsIG1heE9jdXJhbmNlLCB3b3Jkcy5nZXQoY3VycmVudFdvcmQudG9TdHJpbmcoKSkpOwogICAgICAgICAgICAgICAgICAgICAgICBjdXJyZW50V29yZCA9IG5ldyBTdHJpbmdCdWlsZGVyKCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHdhc0xldHRlciA9IGZhbHNlOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGlmIChjdXJyZW50V29yZC5sZW5ndGgoKSA+IDApIHsKICAgICAgICAgICAgICAgICAgICBpbmNyZWFzZUZyZXF1ZW5jeSh3b3JkcywgY3VycmVudFdvcmQudG9TdHJpbmcoKSk7CiAgICAgICAgICAgICAgICAgICAgdXBkYXRlUmVjdXJyZW50V29yZChyZWN1cnJlbnRXb3JkLCBjdXJyZW50V29yZCwgbWF4T2N1cmFuY2UsIHdvcmRzLmdldChjdXJyZW50V29yZC50b1N0cmluZygpKSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgdHJ5IHsKICAgICAgICAgICAgICAgIHJldHVybiByZWN1cnJlbnRXb3JkLnRvU3RyaW5nKCk7CiAgICAgICAgICAgIH0gY2F0Y2ggKEV4Y2VwdGlvbiBlKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gIk51IGFpIGludHJvZHVzIG5pY2ktdW4gdGV4dC4iOwogICAgICAgICAgICB9CiAgICAgICAgfSBjYXRjaCAoRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgcmV0dXJuICJFcm9hcmUgbGEgY2l0aXJlYSB0ZXh0dWx1aS4iOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgQnVmZmVyZWRSZWFkZXIgcmVhZGVyID0gbmV3IEJ1ZmZlcmVkUmVhZGVyKG5ldyBJbnB1dFN0cmVhbVJlYWRlcihTeXN0ZW0uaW4pKTsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oZnJlcXVlc3RXb3JkKHJlYWRlcikpOwogICAgfQp9