This is the first State compression question I have made, and I have understood it myself and understood it thoroughly.
A: A string consisting of several uppercase letters. Then, select as many strings as possible to make these letters appear even.
The simplest idea is to use the following method: Each string can only be selected or not. Therefore, the time complexity of the effort is O (2n)
Optimization: divide the N strings into two halves, enumerate all possible conditions of the first N1 string, calculate the XOR value, and save it in table
Enumerate the XOR values in the second half and search for them in the table (because if the two values are the same, the value after the exclusive or operation is 0). If yes, update ans to a scheme with a large bitcount value.
1 // # define local 2 # include <cstdio> 3 # include <map> 4 using namespace STD; 5 6 const int maxn = 24; 7 Map <int, int> table; 8 9 int bitcount (int x) 10 {// calculate the number of 1 in the binary array x 11 return (x = 0? 0: (X & 1) + bitcount (x> 1); 12} 13 14 int main (void) 15 {16 # ifdef local17 freopen ("2965in.txt ", "r", stdin); 18 # endif19 20 int n, a [maxn]; 21 char s [1000]; 22 while (scanf ("% d", & N) = 1 & N) 23 {24 for (INT I = 0; I <n; ++ I) 25 {26 scanf ("% s", S ); 27 A [I] = 0; 28 for (Int J = 0; s [J]! = '\ 0'; ++ J) // A [I] is the string's binary vector 29 A [I] ^ = (1 <(s [J]-'A ')); 30} 31 int n1 = n/2, n2 = N-N1; 32 33 // enumerate all possible differences or values of the first N1 string 34 table. clear (); 35 for (INT I = 0; I <(1 <N1); ++ I) 36 {37 int x = 0; 38 for (Int J = 0; j <N1; ++ J) 39 if (I & (1 <j) 40 x ^ = A [J]; 41 if (! Table. count (x) | bitcount (Table [x]) <bitcount (I )) 42 // If X is not repeated or the XOR value is the same, select more strings under I. 43 table [x] = I; 44} 45 // enumerate all the combinations of N2 elements and find 46 int ans = 0 in the table; 47 for (INT I = 0; I <(1 <N2); ++ I) 48 {49 int x = 0; 50 for (Int J = 0; j <N2; ++ J) 51 if (I & (1 <j) 52 x ^ = A [N1 + J]; 53 If (table. count (x) & bitcount (ANS) <(bitcount (Table [x]) + bitcount (I ))) 54 // if the value of X exists and more strings are selected in this solution, update the value of ANS 55 ans = (I <N1) ^ table [x]; 56} 57 // output result 58 printf ("% d \ n", bitcount (ANS); 59 for (INT I = 0; I <n; ++ I) 60 {61 If (ANS & (1 <I) 62 printf ("% d", I + 1); 63} 64 printf ("\ n "); 65} 66 return 0; 67}
Code Jun
Here, I also learned the usage of map in a fuzzy way ..