Initial reading input.

master
Tomasz Polgrabia 2025-02-09 21:41:20 +01:00
parent 3c06d3fd45
commit 69ec4d2e56
4 changed files with 96 additions and 10 deletions

View File

@ -1,19 +1,19 @@
BIN_DIR=bin
SRC_DIR=src
DEST=bin/program
SRC=$(wildcard $(SRC_DIR)/*.c)
BIN=$(patsubst $(SRC_DIR)/%.c,$(BIN_DIR)/%.c.o,$(SRC))
SRC=$(wildcard $(SRC_DIR)/*.cpp)
BIN=$(patsubst $(SRC_DIR)/%.cpp,$(BIN_DIR)/%.cpp.o,$(SRC))
all: $(DEST)
.PHONY: clean
$(DEST): $(BIN)
${CC} $(LDFLAGS) -o $@ $^
${CXX} $(LDFLAGS) -o $@ $^
$(BIN_DIR)/%.c.o: $(SRC_DIR)/%.c
$(BIN_DIR)/%.cpp.o: $(SRC_DIR)/%.cpp
@mkdir -p $(BIN_DIR)
$(CC) $(CFLAGS) -c -o $@ $<
$(CXX) $(CFLAGS) -c -o $@ $<
clean:
rm -rf $(BIN_DIR)

View File

@ -0,0 +1,4 @@
13: {14, 15, 100, 9, 3}
16: {32, 1, 9, 3, 5}
19: {15, 29, 2, 6, 8, 7}
24: {7, 10}

View File

@ -1,5 +0,0 @@
#include <stdio.h>
int main() {
return 0;
}

View File

@ -0,0 +1,87 @@
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <set>
#include <map>
const int MAX_BUFFER = 0x1000;
using namespace std;
void process_data(map<int,set<int>> &documents) {
for (auto it1 = documents.begin(); it1 != documents.end(); it1++) {
for (auto it2 = documents.begin(); it2 != documents.end(); it2++) {
if (it1->first == it2->first) {
continue;
}
set<int> intersection;
set_intersection(
it1->second.begin(), it1->second.end(),
it2->second.begin(), it2->second.end(),
inserter(intersection, next(intersection.begin())));
if (intersection.size()) {
fprintf(stderr, "Documents %d and %d have %d common elements\n",
it1->first, it2->first, intersection.size());
}
}
}
}
void process_input(map<int,set<int>> &documents, char *buffer) {
char *it = buffer;
set<int> items;
int doc_id = atoi(it);
fprintf(stderr, "Got document id: %d\n", doc_id);
while (*it && *it != '{') {
it++;
}
++it;
do {
auto nr = atoi(it);
fprintf(stderr, "Got number: %d\n", nr);
items.insert(nr);
while (*it && *it != ',' && *it != '}') {
++it;
}
if (*it == '}') {
break;
}
++it;
} while(true);
fprintf(stderr, "The set contains %d elements\n", items.size());
documents[doc_id] = items;
}
int main() {
char buffer[MAX_BUFFER];
map<int, set<int>> documents;
fprintf(stderr, "Provide input: \n");
while(1) {
if (!fgets(buffer, MAX_BUFFER, stdin)) {
fprintf(stderr, "End of file or error\n");
break;
}
if (strlen(buffer) <= 0 || buffer[0] == '\n') {
break;
}
process_input(documents, buffer);
}
process_data(documents);
fprintf(stderr, "Execution finished\n");
return 0;
}