Bitset Summary (poj2443 & hdu4920)

Source: Internet
Author: User
Tags bit set bitset cmath

I learned how to use bitset, find some bitset usage on the Internet, and call out some common usage.

Constructor
Bitset <n> B;
B has N bits, each of which is 0. Parameter N can be an expression.
For example, bitset <5> B0; then "B0" is "00000 ";

Bitset <n> B (unsigned Long U );
B has N bits and is assigned a value using U. If u exceeds N bits, the top is intercepted.
For example: bitset <5> B0 (5); then "B0" is "00101 ";
 
Bitset <n> B (string S );
B is a copy of the bits contained in String object S.
String bitval ("10011 ");
Bitset <5> B0 (bitval4 );
Then "B0" is "10011 ";

 

Bitset <n> B (S, POs, num );
B is the copy of the num bit starting from the position POs in S. If num <n, the previous vacancy is automatically filled with 0;
String bitval ("11110011011 ");
Bitset <6> B0 (bitval5, 3, 6 );
Then "B0" is "100110 ";


OS <B
Outputs the bitset in B to the OS stream.
OS> B
Input to B, for example, "CIN> B". If the input is not a string of 0 or 1, only the first binary digit is used.

 

Bool any ()
Is there a binary bit set to 1? Opposite to none ()
 
Bool none ()
Is there any binary bit set to 1, that is, all are 0? Opposite to any.
 
Size_t count ()
The number of binary digits 1.
 
Size_t size ()
Number of binary digits

Flip ()
Returns the inverse of all binary values.
 
Flip (size_t POS)
Returns the reversed binary position at the POS.
 
Bool operator [] (size_type POS)
Obtain the binary position at the POS.
 
Set ()
Set all binary bits to 1
 
Set (POS)
Set the binary position at POs to 1.
 
Reset ()
Set all binary bits to 0
 
Reset (POS)
Set the binary position at POs to 0.

 

Note: bitset can only be used with bitset, but not with count.

In addition, find the bitset questions related to the two questions. It is true that bitset can greatly reduce the amount of code after it is used. Poj2443
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <utility>#include <stack>#include <queue>#include <map>#include <deque>#include <bitset>#define max(x,y) ((x)>(y)?(x):(y))#define min(x,y) ((x)<(y)?(x):(y))#define INF 0x3f3f3f3fusing namespace std;bitset<1005> bit[10005];int N,M,T,a,b;int main(){    scanf("%d",&N);    for(int i=0; i<N; i++)    {        scanf("%d",&M);        for(int j=0; j<M; j++)        {            scanf("%d",&a);            bit[a].set(i);        }    }    scanf("%d",&T);    for(int i=0; i<T; i++)    {        scanf("%d%d",&a,&b);        if((bit[a]&bit[b]).any())            printf("Yes\n");        else            printf("No\n");    }    return 0;}
View code

 

Hdu4920:

Idea: Convert the matrix into a matrix with values 0, 1 and 2. The bitset stores values 0, 1 and 2 on the matrix.

You can use count to quickly calculate the number of 1*1 and 2*2, which is easy to get the answer.

Note: The other optimization ideas of this question are to transpose the B matrix, which is said to accelerate the access speed of the array, that is, after the transpose, each access is + 1 rather than + n. the magical effect is self-evident. Of course, it is limited to this constant-level optimization!

 

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <utility>#include <stack>#include <queue>#include <map>#include <deque>#include <bitset>#define max(x,y) ((x)>(y)?(x):(y))#define min(x,y) ((x)<(y)?(x):(y))#define INF 0x3f3f3f3fusing namespace std;bitset<805> A[805][3],B[805][3];int N,k,C;int main(){    while(scanf("%d",&N)!=EOF && N)    {        for(int i=0; i<N; i++)            for(int j=0; j<3; j++)            {                A[i][j].reset();                B[i][j].reset();            }        for(int i=0; i<N; i++)            for(int j=0; j<N; j++)            {                scanf("%d",&k);                A[i][k%3].set(j);            }        for(int i=0; i<N; i++)            for(int j=0; j<N; j++)            {                scanf("%d",&k);                B[j][k%3].set(i);            }        for(int i=0; i<N; i++)            for(int j=0; j<N; j++)            {                C=(A[i][1]&B[j][1]).count()+                    (A[i][1]&B[j][2]).count()*2+(A[i][2]&B[j][1]).count()*2+                    (A[i][2]&B[j][2]).count()*4;                if(j==N-1) printf("%d",C%3);                else printf("%d\n",C%3);            }    }    return 0;}
View code

 

Summary:

Bitset is indeed a powerful tool for bitwise computation. It plays a magical role in compressing various states, but its speed is not very prominent. It takes a long time to test the last two questions, but once used, presumably you are not far from success. Haha !!

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.