কম্পিউটার টিউটোরিয়াল

C++-এ বাইনারি ম্যাট্রিক্সে ডুপ্লিকেট সারি খুঁজুন (Trie ব্যবহার করে)

একটি বাইনারি ম্যাট্রিক্স দেওয়া আছে। এর ডুপ্লিকেট সারিগুলোর পজিশন বের করতে হবে।

উদাহরণ ম্যাট্রিক্স:

110101
001001
101100
110101
001001
001001

ডুপ্লিকেট সারি পজিশন: 3, 4, 5 (০-ভিত্তিক ইনডেক্স)।

Trie-ভিত্তিক সমাধান

বাইনারি ট্রাইতে প্রতিটি সারি ইনসার্ট করা হয়। ইনসার্ট করার সময় যদি সারিটি ইতিমধ্যে থাকে, তবে সেটি ডুপ্লিকেট।

উদাহরণ কোড
#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
  1. C++-এ Find Leaves of Binary Tree: বাইনারি ট্রি থেকে লีফ নোডগুলো লেভেল অনুযায়ী সংগ্রহ করা

  2. C++-এ ডুপ্লিকেট সাবট্রি খুঁজে বের করা

  3. C++ ব্যবহার করে সব ডুপ্লিকেট সাবট্রি বের করা

  4. পাইথন প্রোগ্রাম: বাইনারি ম্যাট্রিক্সে ডুপ্লিকেট সারি খুঁজে বের করা