Given n folders, each folder has several tags and a size. Then m queries are given, and the query is also several tags. It is required to find out which of the tags appear in n folders and ignore the tag search. All the tags add up the size, then output.
Solution: bitset and map can be easily used to record the folders in which each tag appears with a map. map <string, bitset <1024> (4> and> must have a '_'), indicating which of the 1024 folders contains this string, then, when querying, you only need to check which folder contains all the tags, that is, the bitset value corresponding to these tags &. In the end, the folder that is true will accumulate the size.
Let's take a look at the introduction of bitset in Baidu Encyclopedia:
[Cpp]
# Include <bitset>
Bitset // There are three declaration methods. In the default definition, we only need to simply specify the length of the bit vector, for example
Bitset <32> bitvec; // declares the order of 32 bit bitset object bits from 0 to 31 by default, all bits are
// If one or more of the objects are set to 1, any () returns true for bitvec.
Bool is_set = bitvec. any (); // The result is of course false. On the contrary, if all bits of the bitset object are set to 0, the none () operation returns
Bool is_not_set = bitvec. none (); // The result is true count (). The number of BITs set to 1 is returned.
Int bits_set = bitvec. count (); // we can use the set () operation or subscript operator to set a single bit. for example, the for loop below sets the even number to 1.
For (int index = 0; index <32; ++ index)
If (index % 2 = 0)
Bitvec [index] = 1; // similarly, to test whether a single bit is 1, there are two methods to test (). The return value is true when the parameter is set by position.
If (bitvec. test (0) // Our bitve [0] can work! We can also use the subscript operator.
For (int index = 0; index <32; ++ index)
If (bitvec [index])
Cout <index <""; cout <endl;
Bitvec. reset (0 );
Bitvec [0] = 0;
// We can also use the set () and reset () operations to set all bits of the entire bitset object to 1 or 0, you only need to call the corresponding operation without passing the location parameter. for example
Bitvec. reset ();
If (bitvec. none ()! = True)
// Flip () operation to flip the entire bitset object or an independent bit
Bitvec. flip (0); // flip first
Bitvec [0]. flip (); // It is also the first place to flip
Bitvec. flip (); // flip the values of all BITs
// Two other methods can be used to construct bitset objects. Both of them provide the method of initializing a bitset object to 1: one way is to provide the constructor with a digital display of the First N bits of the bitset object of an unsigned parameter as the corresponding bits of the parameter, for example
Bitset <32> bitvec2 (0 xffff );
// Set the low 16-bit bitvec2 to 1
00000000000000001111111111111111
// The following bitvec3 Definition
Bitset <32> bitvec3 (012 );
// Set the values of 1st and 3 to 1. Assume that the position starts counting from 0.
00000000000000000000000000001010
// We can also pass a string parameter representing a set of 0 and 1 to construct a bitset object as follows:
// It is equivalent to bitvec3 Initialization
String bitval ("1010 ");
Bitset <32> bitvec4 (bitval );
// The 1st and 3 bits of bitvec4 and bitvec3 are set to 1 while the other bits are kept to 0.
Bitvec. set ()
After reading these questions and codes, we can find that bitset is quite elegant.
Test data:
5
[Comic] [naruto] [01] 1024
[Comic] [naruto] [02] 1024
[Comic] [inuyasha] [01] 1024
[Comic] [inuyasha] [02] 1024
[Comic] [inuyasha] [03] 1024
3
[Inuyasha]
[Comic] [01]
[Ost]
Out: 3072 \ n 2047 \ n 0 \ n
Code:
[Cpp]
<Span style = "color: # ff0000;" >#include <cstdio>
# Include <map>
# Include <string>
# Include <cstring>
# Include <bitset>
# Include <vector>
Using namespace std;
# Define int64 long
# Deprecision MAX 1300
Int64 n, m, size [MAX], ans;
Bitset <MAX> mask; // mask indicates the main strings in which the string appears.
Char str [1 <17], * p, * q;
Map <string, bitset <MAX> mmap; // indicates which strings in I = 0... n-1 contain string substrings
Map <string, bitset <MAX >:: iterator it; // iterator
Int main ()
{
Int I, j, k;
While (scanf ("% lld", & n )! = EOF ){
Ans = 0;
Mmap. clear ();
For (I = 0; I <n; ++ I ){
Scanf ("% s % lld", str, & size [I]);
P = str; // pay the first address to the p pointer.
While (p = strchr (p ,'['))! = NULL) {// start from p to find the first '['. If no NULL is returned
Q = strchr (p, ']');
String tp = string (p + 1, q); // constructor tp = str [p + 1 ......... q-1]
Mmap [tp]. set (I); // the I-th string contains tp
P = q; // It is not an infinite loop starting from q.
}
}
Scanf ("% lld", & m );
For (I = 0; I <m; ++ I ){
Scanf ("% s", str );
For (j = 0; j <n; ++ j)
Mask. set (j); // equivalent to memset (true)
P = str;
While (p = strchr (p ,'['))! = NULL ){
Q = strchr (p, ']');
String tp = string (p + 1, q );
It = mmap. find (tp); // equivalent to mmap [tp]
If (it = mmap. end ()){
Mask. reset ();
Break;
}
Else mask & = it-> second; // if there is one in it, the substring must be included in that main string.
P = q;
}
Ans = 0;
For (j = 0; j <n; ++ j)
If (mask [j]) ans + = size [j];
Printf ("% lld \ n", ans );
}
}
} </Span>
Author: woshi250hua