[模拟]Jurassic

来源:互联网 发布:t splines mac 编辑:程序博客网 时间:2024/05/16 13:04

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$ \le$N$ \le$24). NextN 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 print L - the maximal possible number of bones that could be used to reassemble skeleton fragments. After that 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 

6ABDEGGEABEACBCD

Sample Output 

51 2 3 5 6


不关心出现的次数,只关心出现次数的奇偶性,奇偶性的叠加用异或运算表示。

使用简单枚举规模太大,注意到最后符合条件的组合,可以用状态0表示,我们可以等分成两部分来解决,当两部分相等时异或值等于0。

这个方法称为中途相遇法。


#include <cstdio>#include <map>#include <iterator>typedef unsigned int ui;using std::map;map<ui,int> hash;map<ui,int>::iterator it1;ui sta[100];int n;int ans = 0;ui ans2 = 0;int calc(int a){int rs = 0;while (a){if (a & 1) rs ++;a >>= 1;}return rs;}int max(int a,int b){if (calc(a) > calc(b))return a;return b;}void dfs(int l,ui s,ui c){hash[s] = max(hash[s],c);if (l > n/2)return;//printf("debug s=%u c=%u\n",s,c);dfs(l+1,s^sta[l],c|(1<<(l-1)) );dfs(l+1,s,c);}void dfs2(int l,ui s,ui c){it1 = hash.find(s);if (it1 != hash.end()){int tmp = calc(c|it1->second);if (ans < tmp){ans = tmp;ans2 = c|it1->second;}}if (l > n)return;dfs2(l+1,s^sta[l],c|(1<<(l-1)) );dfs2(l+1,s,c);}char str[100];int main(){freopen("jurassic.in","r",stdin);freopen("jurassic.out","w",stdout);while (scanf("%d",&n)==1 && n)    {        hash.clear();        for (int i=0;i<=30;i++)            sta[i] = 0;        ans = ans2 = 0;        for (int i=1;i<=n;i++)        {            scanf("%s",str);            char* ptr = str;            while (*ptr)            {                sta[i] ^= (1u<<(*ptr-'A'));                //printf("debug str[%d]=%c\n",ptr-str,*ptr);                ptr ++;            }            //printf("debug sta[%d]=%u\n",i,sta[i]);        }        dfs(1,0,0);        //printf("debug sta[11] = %u\n",sta[11]);        dfs2(n/2+1,0,0);        printf("%d\n",ans);        for (int i=0;i<n;i++)        {            if (ans2 & (1<<i))                printf("%d ",i+1);        }        printf("\n");    }return 0;}



原创粉丝点击