Input:
10101
11100
I would like to store these two lines in a data type so that I can call the |OR operator on two.
Here is my code:
var g = new byte[2][];
g[0] = "10101".Select(item => byte.Parse(item.ToString())).ToArray();
g[1] = "11100".Select(item => byte.Parse(item.ToString())).ToArray();
Console.WriteLine(g[0] | g[1]);
Compiler error I get:
Cannot apply the '|' operator operands byte [] and byte []
I also tried BitArray, but that didn't seem right either. And I tried byte.Parse ("10101"), which just causes an overflow, it makes sense to me.
What I'm trying to do is OR bits of both strings and the result will be = 1, maybe I need to shift the bits in a for loop, I thought maybe I could just OR both binary representations of binary strings of equal length
, , byte [], , , , .
UPDATE
, . , , .
- , HackerRank: https://www.hackerrank.com/challenges/acm-icpc-team
" N , ACM-ICPC World Finals. , . , 2 , , ."
, Stack Overflow, :
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
var inputArr = Console.ReadLine().Split(' ').Select(item => int.Parse(item)).ToArray();
var n = inputArr[0];
var m = inputArr[1];
var g = new byte[n];
var team = new List<byte>();
var teamsKnowMax = 0;
var maxTopics = byte.MinValue >> sizeof(byte) * 8 - m;
for(var i = 0; i < n; i++){
g[i] = Convert.ToByte(Console.ReadLine(), 2);
maxTopics = maxTopics | g[i];
}
for(var j = 0; j < n -1; j++){
for(var k = j+1; k < n; k++){
var or = g[j] | g[k];
if((or & maxTopics) == maxTopics)
teamsKnowMax++;
}
}
Console.WriteLine(Convert.ToString(maxTopics,2).ToCharArray().Count(item => item == '1'));
Console.WriteLine(teamsKnowMax);
}
}
:
2≤N≤500
1≤M≤500
, , 8- , , , , , .
8 , , , . , - . BitArray, -, | OR, . : https://codereview.stackexchange.com/questions/80458/acm-icpc-team-challenge-on-hackerrank-easy
static void Main(String[] args) {
var input = Console.ReadLine().Split(' ').Select(item => int.Parse(item)).ToArray();
var N = input[0];
var M = input[1];
var maxTopics = 0;
var maxTeams = 0;
var bitArray = new BitArray[N];
for(var n = 0; n < N; n++){
bitArray[n] = new BitArray(M);
var topics = Console.ReadLine();
for(var m = 0; m < M; m++){
bitArray[n].Set(m, topics[m] == '1');
}
}
for(int i = 0; i < N -1; i ++){
for(int j = i + 1; j < N; j++){
var tempTopics = BitsOnCount(new BitArray(M).Or(bitArray[i]).Or(bitArray[j]));
if (tempTopics > maxTopics){
maxTopics = tempTopics;
maxTeams = 1;
}else if(tempTopics == maxTopics){
maxTeams++;
}
}
}
Console.WriteLine(maxTopics);
Console.WriteLine(maxTeams);
}
static int BitsOnCount(BitArray bitArray)
{
var count = 0;
foreach (var bit in bitArray)
{
if ((bool) bit)
count++;
}
return count;
}