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];
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.";
}
}
System.
out.
println(frequestWord
(reader
)); }
}
aW1wb3J0IGphdmEuaW8uQnVmZmVyZWRSZWFkZXI7CmltcG9ydCBqYXZhLmlvLklPRXhjZXB0aW9uOwppbXBvcnQgamF2YS5pby5JbnB1dFN0cmV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AgICAgICAgICAgICAgIGlmIChpc0xldHRlcihjdXJyZW50TGluZS5jaGFyQXQoaSkpKSB7CiAgICAgICAgICAgICAgICAgICAgY3VycmVudFdvcmQuYXBwZW5kKGN1cnJlbnRMaW5lLmNoYXJBdChpKSk7CiAgICAgICAgICAgICAgICAgICAgd2FzTGV0dGVyID0gdHJ1ZTsKICAgICAgICAgICAgICAgIH0gZWxzZSBpZiAod2FzTGV0dGVyKSB7CiAgICAgICAgICAgICAgICAgICAgaW5jcmVhc2VGcmVxdWVuY3kod29yZHMsIGN1cnJlbnRXb3JkLnRvU3RyaW5nKCkpOwogICAgICAgICAgICAgICAgICAgIHVwZGF0ZVJlY3VycmVudFdvcmQocmVjdXJyZW50V29yZCwgY3VycmVudFdvcmQsIG1heE9jdXJhbmNlLCB3b3Jkcy5nZXQoY3VycmVudFdvcmQudG9TdHJpbmcoKSkpOwogICAgICAgICAgICAgICAgICAgIGN1cnJlbnRXb3JkID0gbmV3IFN0cmluZ0J1aWxkZXIoKTsKICAgICAgICAgICAgICAgICAgICB3YXNMZXR0ZXIgPSBmYWxzZTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoY3VycmVudFdvcmQubGVuZ3RoKCkgPiAwKSB7CiAgICAgICAgICAgICAgICBpbmNyZWFzZUZyZXF1ZW5jeSh3b3JkcywgY3VycmVudFdvcmQudG9TdHJpbmcoKSk7CiAgICAgICAgICAgICAgICB1cGRhdGVSZWN1cnJlbnRXb3JkKHJlY3VycmVudFdvcmQsIGN1cnJlbnRXb3JkLCBtYXhPY3VyYW5jZSwgd29yZHMuZ2V0KGN1cnJlbnRXb3JkLnRvU3RyaW5nKCkpKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB0cnkgewogICAgICAgICAgICByZXR1cm4gcmVjdXJyZW50V29yZC50b1N0cmluZygpOwogICAgICAgIH0gY2F0Y2ggKEV4Y2VwdGlvbiBlKSB7CiAgICAgICAgICAgIHJldHVybiAiTnUgYWkgaW50cm9kdXMgbmljaS11biB0ZXh0LiI7CiAgICAgICAgfQogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBJT0V4Y2VwdGlvbiwgTnVsbFBvaW50ZXJFeGNlcHRpb24gewogICAgICAgIEJ1ZmZlcmVkUmVhZGVyIHJlYWRlciA9IG5ldyBCdWZmZXJlZFJlYWRlcihuZXcgSW5wdXRTdHJlYW1SZWFkZXIoU3lzdGVtLmluKSk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKGZyZXF1ZXN0V29yZChyZWFkZXIpKTsKICAgIH0KfQ==