在C++中查找二进制矩阵中位差最大的两行
假设我们有一个二进制矩阵;我们必须找到该矩阵中位差最大的两行。
因此,如果输入类似于矩阵,则输出将为 [2,3],因为第 2 行和第 3 行之间的位差为 4,这是最大的。
为了解决这个问题,我们将遵循以下步骤:
定义 Trie 结构,包含值和两个子节点。
定义一个函数 `get_max_bit_diff()`,它将接收 Trie 的根节点、矩阵、n(列数)、行索引作为参数。
temp := 根节点, count := 0
for i := 0 to n-1 do −
如果 temp 的子节点[matrix[row_index, i]] 不为空,则:
temp := temp 的子节点[matrix[row_index, i]]
否则,如果 temp 的子节点[1 - matrix[row_index, i]] 不为空,则:
temp := temp 的子节点[1 - matrix[row_index, i]]
(count 加 1)
leaf_index := temp 的叶子节点索引
temp_count := 0, temp := 根节点
for i := 0 to n-1 do −
如果 temp 的子节点[1 - matrix[row_index, i]] 不为空,则:
temp := temp 的子节点[1 - matrix[row_index, i]]
(temp_count 加 1)
否则,如果 temp 的子节点[matrix[row_index, i]] 不为空,则:
temp := temp 的子节点[matrix[row_index, i]]
P = if temp_count > count then 创建一个包含 (temp_count, temp 的叶子节点索引) 的对,否则创建一个包含 (count, leaf_index) 的对
返回 P
在主方法中,执行以下操作:
root = 一个新的 TrieNode
将第 0 行插入 root
max_bit_diff := -inf
定义一个对 pr 和另一个对 temp
for i := 1 to n-1 do −
temp := get_max_bit_diff(root, mat, m, i)
如果 max_bit_diff < temp.first,则:
max_bit_diff := temp.first
pr := 创建一个包含 (temp.second, i + 1) 的对
将第 i 行插入 root
显示对 pr
示例 (C++)
让我们看看下面的实现,以便更好地理解:
#include<bits/stdc++.h>
using namespace std;
const int MAX = 100;
class TrieNode {
public:
int leaf;
TrieNode *child[2];
TrieNode(){
leaf = 0;
child[0] = child[1] = NULL;
}
};
void insert(TrieNode *root, int matrix[][MAX], int n, int row_index){
TrieNode * temp = root;
for (int i=0; i<n; i++) {
if(temp->child[ matrix[row_index][i] ] == NULL)
temp->child[ matrix[row_index][i] ] = new TrieNode();
temp = temp->child[ matrix[row_index][i] ];
}
temp->leaf = row_index +1 ;
}
pair<int, int>get_max_bit_diff(TrieNode * root, int matrix[][MAX], int n, int row_index) {
TrieNode * temp = root;
int count = 0;
for (int i= 0 ; i < n ; i++) {
if (temp->child[ matrix[row_index][i] ] != NULL)
temp = temp->child[ matrix[row_index][i] ];
else if (temp->child[1 - matrix[row_index][i]] != NULL) {
temp = temp->child[1- matrix[row_index][i]];
count++;
}
}
int leaf_index = temp->leaf;
int temp_count = 0 ;
temp = root;
for (int i= 0 ; i < n ; i++) {
if (temp->child[ 1 - matrix[row_index][i] ] !=NULL) {
temp = temp->child[ 1- matrix[row_index][i] ];
temp_count++;
}
else if (temp->child[ matrix[row_index][i] ] != NULL)
temp = temp->child[ matrix[row_index][i] ];
}
pair <int ,int> P = temp_count > count ? make_pair(temp_count, temp->leaf): make_pair(count, leaf_index);
return P;
}
void get_max_diff( int mat[][MAX], int n, int m) {
TrieNode * root = new TrieNode();
insert(root, mat, m, 0);
int max_bit_diff = INT_MIN;
pair<int ,int> pr, temp ;
for (int i = 1 ; i < n; i++) {
temp = get_max_bit_diff(root, mat, m ,i);
if (max_bit_diff < temp.first ) {
max_bit_diff = temp.first;
pr = make_pair( temp.second, i+1);
}
insert(root, mat, m, i );
}
cout << "(" << pr.first <<", "<< pr.second << ")";
}
int main() {
int mat[][MAX] = {
{1 ,1 ,1 ,1 },
{1, 0, 1 ,1},
{0 ,1 ,0 ,0},
{1 ,0 ,0 ,0}
};
get_max_diff(mat, 4, 4) ;
}输入
{{1 ,1 ,1 ,1 },
{1, 0, 1 ,1},
{0 ,1 ,0 ,0},
{1 ,0 ,0 ,0}}, 4,4输出
(2,3)
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP