#include <unordered_map>
#include <iostream>
#include <vector>
#include <set>
void findRepeatingPatterns(const std::vector<std::vector<int>>& matrix, int patternSize) {
std::unordered_map<std::string, std::set<int>> patternMap;
for (int row = 0; row < matrix.size(); row++) {
for (int col = 0; col <= matrix[row].size() - patternSize; col++) {
std::string pattern = "";
for (int i = 0; i < patternSize; ++i) {
pattern += std::to_string(matrix[row][col + i]) + "-";
}
patternMap[pattern].insert(row); // Row index starts from 0
}
}
std::cout << "Repeated Patterns Found:\n";
for (const auto& entry : patternMap) {
if (entry.second.size() > 1) {
std::cout << entry.first << " appears " << entry.second.size() << " times in rows: ";
for (int row : entry.second) {
std::cout << row << " ";
}
std::cout << std::endl;
}
}
}
int main() {
std::vector<std::vector<int>> matrix = {
{1, 2, 3, 8, 9, 7, 4, 9, 6},
{1, 3, 2, 7, 8, 9, 4, 5, 6},
{1, 2, 3, 8, 6, 1, 4, 9, 8},
{1, 2, 3, 0, 8, 8, 4, 5, 9},
{1, 2, 3, 4, 5, 6, 7, 8, 9},
{1, 2, 3, 7, 0, 9, 4, 5, 7},
{1, 3, 2, 4, 5, 6, 7, 8, 9}
};
int patternSize = 3; // Looking for sequences of 3 numbers
findRepeatingPatterns(matrix, patternSize);
}
/*
run:
Repeated Patterns Found:
9-4-5- appears 2 times in rows: 1 5
7-8-9- appears 3 times in rows: 1 4 6
1-2-3- appears 5 times in rows: 0 2 3 4 5
4-5-6- appears 3 times in rows: 1 4 6
1-3-2- appears 2 times in rows: 1 6
2-3-8- appears 2 times in rows: 0 2
5-6-7- appears 2 times in rows: 4 6
6-7-8- appears 2 times in rows: 4 6
*/