A subset of a subset of a binary drawing

I have A = a set of rows and B = a separate row. I want to count the number of occurrences from B to A.

Example:

A:
10001
10011
11000
10010
10101

B:
10001

result would be 3.(10001 is a subset of 10001,10011,10101)

So, I need a function that takes a set and a string and returns an int.

int myfunc(set<string> , string){
int result;
// My Brain is melting
return result ;
}

edit:  00000 should not be a subset of anything!

+3
source share
4 answers

, , , , , . , , , .

, , , . Offhand, - :

int myfunc(set<string> in, string search){
    assert(search.length() <= 32);
    int result = 0;
    for(set<string>::iterator iIn = in.begin(); iIn != in.end(); ++iIn)
    {
       bool isSubset = true;
       if (iIn->length() != search.length()) // Is this guaranteed?
           isSubset = false;
       for (int iSearch = 0; isSubset && iSearch < search.length; ++iSearch)
           if (search[iSearch] == '1' && (*iIn)[iSearch] == '0')
               isSubset = false;
       if (isSubset)
           ++result;
    }
    return result;
}

:

int myfunc(set<string> in, string search){
    int result = 0;
    long searchInteger = strtol(search.c_str(), NULL, 2);
    for(set<string>::iterator iIn = in.begin(); iIn != in.end(); ++iIn)
        if ((strtol(iIn->c_str(), NULL, 2) & searchInteger) == searchInteger)
            ++result;
    return result;
}
+2

:

if ((input & mask) == mask) {
    /* matches */
}
+2

and :

if( ( pattern & listItem[i] ) == pattern )
{
  // match
}

, pattern listItem [i] , and.

+2
source

Can we assume that the strings are the same length and consist only of characters 0 and 1?

Well, yes, then if you can find a function to convert a binary string to an integer, then doing as others suggested, using the "and" operation is the way to go.

Otherwise, something like this is possible:

int count = 0;
for (k=0; k<sizeofA; k++) {
  for (j=0; j<lengthOfString; j++)
      if ( ('1'==B[j]) && ('1' != A[k][j])) break;
  if (j==lengthOfString) count++;
}
+1
source

Source: https://habr.com/ru/post/1739036/


All Articles