একটি বাইনারি ম্যাট্রিক্স দেওয়া আছে। এর ডুপ্লিকেট সারিগুলোর পজিশন বের করতে হবে।
উদাহরণ ম্যাট্রিক্স:
| 1 | 1 | 0 | 1 | 0 | 1 |
| 0 | 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 | 0 | 1 |
| 0 | 0 | 1 | 0 | 0 | 1 |
| 0 | 0 | 1 | 0 | 0 | 1 |
ডুপ্লিকেট সারি পজিশন: 3, 4, 5 (০-ভিত্তিক ইনডেক্স)।
বাইনারি ট্রাইতে প্রতিটি সারি ইনসার্ট করা হয়। ইনসার্ট করার সময় যদি সারিটি ইতিমধ্যে থাকে, তবে সেটি ডুপ্লিকেট।
#include <iostream>
using namespace std;
const int MAX = 100;
class Trie {
public:
bool leaf_node;
Trie* children[2];
};
Trie* getNode() {
Trie* node = new Trie;
node->children[0] = node->children[1] = NULL;
node->leaf_node = false;
return node;
}
bool insert(Trie*& head, bool* arr, int N) {
Trie* curr = head;
for (int i = 0; i < N; i++) {
if (curr->children[arr[i]] == NULL)
curr->children[arr[i]] = getNode();
curr = curr->children[arr[i]];
}
if (curr->leaf_node)
return false;
return (curr->leaf_node = true);
}
void displayDuplicateRows(bool matrix[][MAX], int M, int N) {
Trie* head = getNode();
for (int i = 0; i < M; i++)
if (!insert(head, matrix[i], N))
cout << "There is a duplicate row at position: " << i << endl;
}
int main() {
bool mat[][MAX] = {
{1, 1, 0, 1, 0, 1},
{0, 0, 1, 0, 0, 1},
{1, 0, 1, 1, 0, 0},
{1, 1, 0, 1, 0, 1},
{0, 0, 1, 0, 0, 1},
{0, 0, 1, 0, 0, 1}
};
displayDuplicateRows(mat, 6, 6);
}
There is a duplicate row at position: 3 There is a duplicate row at position: 4 There is a duplicate row at position: 5