LA2965 Jurassic Remains
Jurassic Remains
https://vjudge.net/problem/UVALive-2965
Paleontologists in Siberia have recently found a number of fragments of Jurassic period dinosaur skeleton. The paleontologists have decided to forward them to the paleontology museum. Unfortunately, the dinosaur was so huge, that there was no box that the fragments would fit into. Therefore it was decided to split the skeleton fragments into separate bones and forward them to the museum where they would be reassembled. To make reassembling easier, the joints where the bones were detached from each other were marked with special labels. Meanwhile, after packing the fragments, the new bones were found and it was decided to send them together with the main fragments. So the new bones were added to the package and it was sent to the museum. However, when the package arrived to the museum some problems have shown up. First of all, not all labels marking the joints were distinct. That is, labels with letters ‘A’ to ‘Z’ were used, and each two joints that had to be connected were marked with the same letter, but there could be several pairs of joints marked with the same letter. Moreover, the same type of labels was used to make some marks on the new bones added to the box. Therefore, there could be bones with marked joints that need not be connected to the other bones. The problem is slightly alleviated by the fact that each bone has at most one joint marked with some particular letter. Your task is to help the museum workers to restore some possible dinosaur skeleton fragments. That is, you have to find such set of bones, that they can be connected to each other, so that the following conditions are true: • If some joint is connected to the other joint, they are marked with the same label. • For each bone from the set each joint marked with some label is connected to some other joint. • The number of bones used is maximal possible. Note that two bones may be connected using several joints. Input Input consists of several datasets. The first line of each dataset contains N — the number of bones (1 ≤ N ≤ 24). Next N lines contain bones descriptions: each line contains a non-empty sequence of different capital letters, representing labels marking the joints of the corresponding bone. Output For each dataset, on the first line of the output file print L — the maximal possible number of bones that could be used to reassemble skeleton fragments. After that, in another line, output L integer numbers in ascending order — the bones to be used. Bones are numbered starting from one, as they are given in the input file. Sample Input 1 ABC 6 ABD EG GE ABE AC BCD Sample Output 0 5 1 2 3 5 6
把每个字符串中某个字符是否出现用0/1表示,只需求选出最多的字符串异或为0即可meet in the middle即可
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <map> 8 #include <vector> 9 #define min(a, b) ((a) < (b) ? (a) : (b)) 10 #define max(a, b) ((a) > (b) ? (a) : (b)) 11 #define abs(a) ((a) < 0 ? (-1 * (a)) : (a)) 12 inline void swap(int &a, int &b) 13 { 14 int tmp = a;a = b;b = tmp; 15 } 16 inline void read(int &x) 17 { 18 x = 0;char ch = getchar(), c = ch; 19 while(ch < '0' || ch > '9') c = ch, ch = getchar(); 20 while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar(); 21 if(c == '-') x = -x; 22 } 23 24 const int INF = 0x3f3f3f3f; 25 const int MAXN = 30; 26 27 int n, num[MAXN]; 28 std::map<int, int> mp; 29 char s[MAXN]; 30 31 int k,l1,l2; 32 33 int main() 34 { 35 while(scanf("%d", &n) != EOF) 36 { 37 k = l1 = l2 = 0;mp.clear(); 38 memset(num, 0, sizeof(num)); 39 for(register int i = 1;i <= n;++ i) 40 { 41 scanf("%s", s + 1); 42 for(register int j = 1;s[j] != '\0';++ j) 43 num[i] |= (1 << (s[j] - 'A')); 44 } 45 int ma = 1 << (n / 2); 46 for(register int i = 0;i < ma;++ i) 47 { 48 int ans = 0; 49 for(register int j = 0;j < n/2;++ j) 50 if((1 << j) & i) ans ^= num[j + 1]; 51 mp[ans] = i; 52 } 53 ma = 1 << (n - (n / 2)); 54 for(register int i = 0;i < ma;++ i) 55 { 56 int ans = 0; 57 for(register int j = 0;j < n - n/2;++ j) 58 if((1 << j) & i) ans ^= num[n/2 + j + 1]; 59 if(mp[ans]) 60 { 61 int tmp = 0; 62 for(register int j = mp[ans];j;j >>= 1) 63 if(j&1) ++ tmp; 64 for(register int j = i;j;j >>= 1) 65 if(j&1) ++ tmp; 66 if(tmp > k) k = tmp, l1 = mp[ans], l2 = i; 67 } 68 } 69 printf("%d\n", k); 70 for(register int i = 1;i <= n/2;++ i) 71 if(l1 & (1 << (i - 1))) printf("%d ", i); 72 for(register int i = n/2 + 1;i <= n;++ i) 73 if(l2 & (1 << (i - n/2 - 1))) printf("%d ", i); 74 putchar('\n'); 75 } 76 return 0; 77 }