diff --git a/README.md b/README.md index ec1f103..9877cb0 100644 --- a/README.md +++ b/README.md @@ -352,7 +352,7 @@ include_toc: true * *FastSort* ✅ * Пусть _n_ – число слов во входном файле, тогда программа должна работать за O(_n_ log _n_). - * *3637* + * *3637* ✅ * Назовём _серединой слова_ подстроку, полученную удалением первых и последних 3 символов слова. Слова длины меньшей 7 игнорируются. diff --git a/java/wordStat/WordStatLengthAffix.java b/java/wordStat/WordStatLengthAffix.java new file mode 100644 index 0000000..47990c4 --- /dev/null +++ b/java/wordStat/WordStatLengthAffix.java @@ -0,0 +1,94 @@ +import java.io.*; +import java.util.*; + +public class WordStatLengthAffix { + public static void main(String[] args) { + if (args.length != 2) { + System.err.println("incorrect input!"); + System.err.println("usage: java WordStat inputFile outputFile"); + } + + String inputFileName = args[0]; + String outputFileName = args[1]; + try { + BufferedReader r = new BufferedReader(new FileReader(inputFileName)); + + Map wordMap = new HashMap<>(); + StringBuilder sb = new StringBuilder(); + int wordIndex = 0; + + int data = r.read(); + while (data != -1) { + char c = (char) data; + + if (Character.getType(c) == Character.DASH_PUNCTUATION || + Character.isLetter(c) || c == '\'') { + sb.append(c); + } else { + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() != 1) { + String prefix = word.substring(0, word.length() / 2); + String suffix = word.substring(word.length() - word.length() / 2); + if (wordMap.containsKey(prefix)) { + wordMap.get(prefix).count++; + } else { + wordMap.put(prefix, new WordInfo(prefix, 1, wordIndex)); + wordIndex++; + } + if (wordMap.containsKey(suffix)) { + wordMap.get(suffix).count++; + } else { + wordMap.put(suffix, new WordInfo(suffix, 1, wordIndex)); + wordIndex++; + } + + } + sb.setLength(0); + } + } + + data = r.read(); + } + + + if (sb.length() > 0) { + String word = sb.toString().toLowerCase(); + if (word.length() != 1) { + String prefix = word.substring(0, word.length() / 2); + String suffix = word.substring(word.length() - word.length() / 2); + if (wordMap.containsKey(prefix)) { + wordMap.get(prefix).count++; + } else { + wordMap.put(prefix, new WordInfo(prefix, 1, wordIndex)); + wordIndex++; + } + if (wordMap.containsKey(suffix)) { + wordMap.get(suffix).count++; + } else { + wordMap.put(suffix, new WordInfo(suffix, 1, wordIndex)); + wordIndex++; + } + } + } + + r.close(); + + List sortedWords = new ArrayList<>(wordMap.values()); + sortedWords.sort(Comparator.comparingInt((WordInfo w) -> w.word.length()) + .thenComparingInt(w -> w.firstIndex)); + + + PrintWriter writer = new PrintWriter(outputFileName, "UTF-8"); + + for (WordInfo info : sortedWords) { + writer.println(info.word + " " + info.count); + } + + writer.close(); + + } catch (Exception ex) { + System.err.println("An error occured: " + ex.getMessage()); + } + } +}