Files
contests/2019/republic/11_d2/experimente.cpp
2022-06-01 20:14:50 +03:00

153 lines
2.6 KiB
C++

#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;
ifstream input_stream;
ofstream output_stream;
int matchCode(string code1, string code2) {
int match = 0;
const int lim = min(code1.length(), code2.length());
for (int i = 0; i < lim; i++) {
if (code1[i] == code2[i]) {
match++;
}
}
return match;
}
void task1() {
uint16_t n;
input_stream >> n;
uint16_t m;
input_stream >> m;
uint16_t G;
input_stream >> G;
string monea_code;
input_stream >> monea_code;
int16_t best_match = -1;
vector<string> matches;
matches.reserve(m * n);
for (uint16_t y = 0; y < n; y++) {
for (uint16_t x = 0; x < m; x++) {
string cat_code;
input_stream >> cat_code;
uint8_t match = matchCode(monea_code, cat_code);
if (match > best_match && match > G) {
best_match = match;
matches.clear();
matches.push_back(to_string(y + 1) + " " + to_string(x + 1) + " " + cat_code);
} else if (match == best_match) {
matches.push_back(to_string(y + 1) + " " + to_string(x + 1) + " " + cat_code);
}
}
}
if (best_match > 0) {
output_stream << best_match << endl;
for (auto i = matches.begin(); i != matches.end(); i++) {
output_stream << *i << endl;
}
} else {
output_stream << "MONEA NU ARE RUDE" << endl;
}
}
void task2() {
uint16_t n;
input_stream >> n;
uint16_t m;
input_stream >> m;
uint16_t G;
input_stream >> G;
string monea_code;
input_stream >> monea_code;
bool town[n][m] = {};
for (uint16_t y = 0; y < n; y++) {
for (uint16_t x = 0; x < m; x++) {
string cat_code;
input_stream >> cat_code;
if (matchCode(monea_code, cat_code) > G) {
town[y][x] = true;
} else {
town[y][x] = false;
}
}
}
uint16_t sum[n][m] = {};
uint16_t max_of_s, max_i, max_j = 0;
for (uint16_t i = 0; i < n; i++) {
sum[i][m - 1] = town[i][m - 1];
}
for (uint16_t j = 0; j < m; j++) {
sum[n - 1][j] = town[n - 1][j];
}
for (int16_t i = n-2; i >= 0; i--) {
for (int16_t j = m-2; j >= 0; j--) {
if (town[i][j]) {
sum[i][j] = min(sum[i][j+1], min(sum[i+1][j], sum[i+1][j+1])) + 1;
if (sum[i][j] >= max_of_s) {
max_of_s = sum[i][j];
max_i = i;
max_j = j;
}
} else {
sum[i][j] = 0;
}
}
}
if (max_of_s > 0) {
output_stream << (max_i + 1) << " " << (max_j + 1) << " " << max_of_s << endl;
} else {
output_stream << "MONEA NU ARE RUDE" << endl;
}
}
int main() {
input_stream.open("input.txt");
output_stream.open("output.txt");
char task;
input_stream >> task;
if (task == '1') {
task1();
} else {
task2();
}
input_stream.close();
output_stream.close();
return 0;
}