📌  相关文章
📜  在二进制矩阵中查找重复行

📅  最后修改于: 2022-05-13 01:57:23.648000             🧑  作者: Mango

在二进制矩阵中查找重复行

给定一个元素只有 0 和 1 的二进制矩阵,我们需要打印与矩阵中已经存在的行重复的行。
例子:

Input : {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}.

Output :
There is a duplicate row at position: 4 
There is a duplicate row at position: 5 
There is a duplicate row at position: 6 

这个问题主要是在二进制矩阵中查找唯一行的扩展。
一个简单的解决方案是一一遍历所有行。对于每一行,检查它是否存在于其他任何地方。如果是,则打印该行。
时间复杂度:O(ROW^2 x COL)
辅助空间:O(1)
使用 Trie 的最佳解决方案Trie 是一种高效的数据结构,用于存储和检索字符集较小的数据。搜索复杂度作为密钥长度是最佳的。
解决该问题的方法是首先将矩阵插入二进制树中,然后如果新添加的行已经存在于树中,那么我们现在将它是重复行

C
// C++ program to find duplicate rows
// in a binary matrix.
#include
 
const int MAX = 100;
 
/*struct the Trie*/
struct Trie
{
    bool leaf;
    Trie* children[2];
};
 
/*function to get Trienode*/
Trie* getNewTrieNode()
{
    Trie* node = new Trie;
    node->children[0] = node->children[1] = NULL;
    node->leaf = false;
    return node;
}
 
/* function to insert a row in Trie*/
bool insert(Trie*& head, bool* arr, int N)
{
    Trie* curr = head;
 
    for (int i = 0; i < N; i++)
    {
        /*creating a new path if it don not exist*/
        if (curr->children[arr[i]] == NULL)
            curr->children[arr[i]] = getNewTrieNode();
 
        curr = curr->children[arr[i]];
    }
 
    /*if the row already exist return false*/
    if (curr->leaf)
        return false;
 
    /* making leaf node tree and return true*/
    return (curr->leaf = true);
}
 
void printDuplicateRows(bool mat[][MAX], int M, int N)
{
    Trie* head = getNewTrieNode();
 
    /*inserting into Trie and checking for duplicates*/
    for (int i = 0; i < M; i++)
 
        // If already exists
        if (!insert(head, mat[i], N))
            printf("There is a duplicate row"
                  " at position: %d \n", i+1);
 
}
 
/*driver function to check*/
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},
    };
 
    printDuplicateRows(mat, 6, 6);
    return 0;
}


C++
#include
#include
#include
using namespace std;
vector repeatedRows(vector> matrix, int M, int N)
{
     
    sets;
     
    // vector to store the repeated rows
    vectorres;
     
    for(int i=0;i>matrix={
        {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},};
     
  int m=matrix.size();
  int n=matrix[0].size();
  vectorres=repeatedRows(matrix,m,n);
  for(int e:res){
     cout<< "There is a duplicate row at position: "<


输出:

There is a duplicate row at position: 4 
There is a duplicate row at position: 5 
There is a duplicate row at position: 6 

另一种不使用 Trie 但不适用于大量列的方法
另一种方法是转换行的十进制等效值并检查新行是否具有相同的十进制等效值然后它是重复行。如果列数很大,它将不起作用。

这是上述方法的实现。

C++

#include
#include
#include
using namespace std;
vector repeatedRows(vector> matrix, int M, int N)
{
     
    sets;
     
    // vector to store the repeated rows
    vectorres;
     
    for(int i=0;i>matrix={
        {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},};
     
  int m=matrix.size();
  int n=matrix[0].size();
  vectorres=repeatedRows(matrix,m,n);
  for(int e:res){
     cout<< "There is a duplicate row at position: "<
输出
There is a duplicate row at position: 4
There is a duplicate row at position: 5
There is a duplicate row at position: 6

时间复杂度=O(M*N)

空间复杂度=O(M) 其中 M 是行数