Test instructions: Given the type of disease of n cattle, there are a total of M-type diseases, the number of cattle to find the most K-type disease;
Idea: Binary enumeration (diseased place is 1, otherwise 0, such as got 2 12 kinds of disease, the Code is 011 (decimal is 3)), first of all the number of 1 is equal to the binary number of K, and then with all the bull's code to compare, meet the +1, find out and the largest; is to convert 2 into trouble, using bit Well realized, but the bit operation is not very clear meaning, understand the supplementary;
Knowledge Points:
- 3 & 2 = 2, same as 1, different 0, 011 & 010 = 010; (How to use this feature is not clear);
- T |= (1 << (b-1)), which means T equals B converted to binary, then converted to decimal, or, the same is 0, the difference is 1, here is equivalent to the binary sum.
1#include <iostream>2#include <cstdlib>3#include <cstring>4#include <cstdio>5 using namespacestd;6 #defineMAXN 10057 intN, D, K;8 intCOW[MAXN];9 Ten intOkinta) One { A intRET =0; - while(a) - { theRET + = (A &1); -A >>=1; - } - returnret = = k;///if a is converted to a binary with K 1, return K, otherwise return 0 + } - + intMain () A { atscanf"%d%d%d", &n, &d, &k); - for(inti =0; I < n; i++) - { - intA, B; -scanf"%d", &a); - for(intj =0; J < A; J + +) in { -scanf"%d", &b); toCow[i] |= (1<< (b-1)); +cout<<cow[i]<<Endl; - ///Cow[i] Represents a 2-binary representation of a cow's disease and then converts to a decimal the ///if 1 2, that is 010 (cow[i] = 2), the second position is 1 * ///if 2 1 2 is 011 (cow[i] = 3), the 12th position is 1 $ }Panax Notoginseng } - ints = (1<< k)-1; the intE = s << (d-k);///namely s* (2^ (d-k)) + intAns =0; A for(inti = s; I <= e; i++) the { + if(OK (i))///if the cow has K-type disease, enter the next level - { $ inttemp =0; $ for(intj =0; J < N; J + +)///enumerate each cow, match the plus 1 - { - ///don't understand if the if((I & cow[j]) = = Cow[j])///are converted to 2 binary, bitwise AND, the binary number is only smaller than I to meet -temp++;Wuyi } theAns =max (ans, temp); - } Wu } -printf"%d\n", ans); About return 0; $}
View Code
POJ 24,362 Binary enumeration + bitwise operation