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 = new StringBuilder();
int[] maxOcurance = new int[1];
StringBuilder currentWord = new StringBuilder();
boolean wasLetter = false;
while (reader.ready()) {
char letter = (char)reader.read();
if (isLetter(letter)) {
currentWord.append(letter);
wasLetter = true;
} else if (wasLetter) {
increaseFrequency(words, currentWord.toString());
updateRecurrentWord(recurrentWord, currentWord, maxOcurance, words.get(currentWord.toString()));
currentWord = new StringBuilder();
wasLetter = false;
}
}
return recurrentWord.toString();
}
System.
out.
println(frequestWord
(reader
)); }
}
aW1wb3J0IGphdmEuaW8uQnVmZmVyZWRSZWFkZXI7CmltcG9ydCBqYXZhLmlvLklPRXhjZXB0aW9uOwppbXBvcnQgamF2YS5pby5JbnB1dFN0cmVhbVJlYWRlcjsKaW1wb3J0IGphdmEudXRpbC5NYXA7CmltcG9ydCBqYXZhLnV0aWwuSGFzaE1hcDsKCnB1YmxpYyBjbGFzcyBNYWluIHsKICAgIHByaXZhdGUgc3RhdGljIGJvb2xlYW4gaXNMZXR0ZXI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F0ZVJlY3VycmVudFdvcmQocmVjdXJyZW50V29yZCwgY3VycmVudFdvcmQsIG1heE9jdXJhbmNlLCB3b3Jkcy5nZXQoY3VycmVudFdvcmQudG9TdHJpbmcoKSkpOwogICAgICAgICAgICAgICAgY3VycmVudFdvcmQgPSBuZXcgU3RyaW5nQnVpbGRlcigpOwogICAgICAgICAgICAgICAgd2FzTGV0dGVyID0gZmFsc2U7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlY3VycmVudFdvcmQudG9TdHJpbmcoKTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB0aHJvd3MgSU9FeGNlcHRpb24gewogICAgICAgIEJ1ZmZlcmVkUmVhZGVyIHJlYWRlciA9IG5ldyBCdWZmZXJlZFJlYWRlcihuZXcgSW5wdXRTdHJlYW1SZWFkZXIoU3lzdGVtLmluKSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGZyZXF1ZXN0V29yZChyZWFkZXIpKTsKICAgIH0KfQ==