Files
prog-intro-2025/java/wspp/WsppPos.java
me 3678af68c0
Some checks failed
Fast Reverse Tests / test (push) Successful in 24s
Reverse Tests / test (push) Successful in 8s
Sum Tests / test (push) Successful in 8s
Word Stat Tests / test (push) Successful in 7s
Word Stat++ Tests / test (push) Failing after 10s
add solutions for hw6:3233
2026-02-02 14:31:39 +03:00

93 lines
3.3 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package wspp;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class WsppPos {
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Usage: java WsppPos inputFile outputFile");
return;
}
String inputFile = args[0];
String outputFile = args[1];
try {
// Первый проход: считаем количество слов в каждой строке
WordScanner scanner = new WordScanner(inputFile);
Map<Integer, Integer> wordsPerLine = new HashMap<>();
while (scanner.hasNextWord()) {
scanner.nextWord();
int line = scanner.getLineNumber();
wordsPerLine.put(line, wordsPerLine.getOrDefault(line, 0) + 1);
}
scanner.close();
// Второй проход: собираем статистику
scanner = new WordScanner(inputFile);
Map<String, WordInfo> wordMap = new LinkedHashMap<>();
Map<Integer, Integer> currentPosInLine = new HashMap<>();
while (scanner.hasNextWord()) {
String word = scanner.nextWord();
int lineNum = scanner.getLineNumber();
// Позиция с начала строки
int posInLine = currentPosInLine.getOrDefault(lineNum, 0) + 1;
currentPosInLine.put(lineNum, posInLine);
// Пересчитываем в позицию с конца строки
int totalWordsInLine = wordsPerLine.get(lineNum);
int posFromEnd = totalWordsInLine - posInLine + 1;
if (wordMap.containsKey(word)) {
WordInfo info = wordMap.get(word);
info.count++;
info.lineNumbers.add(lineNum);
info.positions.add(posFromEnd);
} else {
WordInfo info = new WordInfo();
info.count = 1;
info.lineNumbers = new IntList();
info.positions = new IntList();
info.lineNumbers.add(lineNum);
info.positions.add(posFromEnd);
wordMap.put(word, info);
}
}
scanner.close();
// Запись результата
PrintWriter writer = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream(outputFile), StandardCharsets.UTF_8));
for (Map.Entry<String, WordInfo> entry : wordMap.entrySet()) {
String word = entry.getKey();
WordInfo info = entry.getValue();
writer.print(word + " " + info.count);
for (int i = 0; i < info.lineNumbers.size(); i++) {
writer.print(" " + info.lineNumbers.get(i) + ":" + info.positions.get(i));
}
writer.println();
}
writer.close();
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
static class WordInfo {
int count;
IntList lineNumbers;
IntList positions;
}
}