using
System;
public
class
TrieNode
{
public
int
Leaf;
public
TrieNode[] Child;
public
TrieNode()
{
Leaf = 0;
Child =
new
TrieNode[2];
Child[0] = Child[1] =
null
;
}
}
public
class
MaxBitDifference
{
private
const
int
MAX = 100;
private
static
TrieNode GetNode()
{
return
new
TrieNode();
}
private
static
void
Insert(TrieNode root,
int
[,] mat,
int
n,
int
rowIndex)
{
var
temp = root;
for
(
var
i = 0; i < n; i++)
{
if
(temp.Child[(mat[rowIndex, i])] ==
null
)
{
temp.Child[(mat[rowIndex, i])] = GetNode();
}
temp = temp.Child[(mat[rowIndex, i])];
}
temp.Leaf = rowIndex + 1;
}
private
static
Tuple<
int
,
int
> MaxBitDiffCount(TrieNode root,
int
[,] mat,
int
n,
int
rowIndex)
{
var
temp = root;
var
count = 0;
for
(
var
i = 0; i < n; i++)
{
if
(temp.Child[(mat[rowIndex, i])] !=
null
)
{
temp = temp.Child[(mat[rowIndex, i])];
}
else
if
(temp.Child[1 - mat[rowIndex, i]] !=
null
)
{
temp = temp.Child[1 - mat[rowIndex, i]];
count++;
}
}
var
leafIndex = temp.Leaf;
var
count1 = 0;
temp = root;
for
(
var
i = 0; i < n; i++)
{
if
(temp.Child[1 - mat[rowIndex, i]] !=
null
)
{
temp = temp.Child[1 - mat[rowIndex, i]];
count1++;
}
else
if
(temp.Child[(mat[rowIndex, i])] !=
null
)
{
temp = temp.Child[(mat[rowIndex, i])];
}
}
var
P = count1 > count ?
new
Tuple<
int
,
int
>(count1, temp.Leaf) :
new
Tuple<
int
,
int
>(count, leafIndex);
return
P;
}
public
static
void
MaxDiff(
int
[,] mat,
int
n,
int
m)
{
var
root = GetNode();
Insert(root, mat, m, 0);
var
maxBitDiff =
int
.MinValue;
Tuple<
int
,
int
> P =
null
, temp =
null
;
for
(
var
i = 1; i < n; i++)
{
temp = MaxBitDiffCount(root, mat, m, i);
if
(maxBitDiff < temp.Item1)
{
maxBitDiff = temp.Item1;
P =
new
Tuple<
int
,
int
>(temp.Item2, i + 1);
}
Insert(root, mat, m, i);
}
Console.WriteLine(
"({0}, {1})"
, P.Item1, P.Item2);
}
public
static
void
Main()
{
var
mat =
new
int
[3, 5] {{0, 1, 0, 1, 0}, {1, 0, 1, 1, 0}, {0, 0, 1, 0, 1}};
MaxDiff(mat, 3, 5);
}
}