码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive - 2965 Jurassic Remains

时间:2014-06-04 20:16:27      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

Time Limit: 18000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

bubuko.com,布布扣
 

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 (1bubuko.com,布布扣Nbubuko.com,布布扣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 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

6
ABD
EG
GE
ABE
AC
BCD

 

Sample Output

5
1 2 3 5 6

::中途相遇法

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <map>
 6 using namespace std;
 7 #define REP(i,n) for(int i=0; i<(n); i++)
 8 #define FOR(i,s,t) for(int i=(s); i<(t); i++)
 9 typedef long long ll;
10 const int N = 26;
11 map<int , int > table;
12 int A[N],n;
13 
14 int bitcount(int x){//计算十进制数转化成2进制后1的个数
15     return x==0? 0: bitcount(x/2) + (x&1);
16 }
17 
18 void solve(){
19     char s[1010];
20     REP(i,n){
21         scanf("%s", s);
22         A[i] = 0;
23         for(int j=0; s[j] != \0; j++) A[i] ^= (1<<(s[j] - A));
24     }
25 
26     table.clear();
27     int n1 = n/2 , n2 = n-n1;
28     REP(i,1<<n1){//1<<n1=2^n1种情况,枚举前n1个字符串的所有情况,如i = 3,二进制为011表示选前两个字符串的情况
29           int x = 0;
30           REP(j,n1) if(i & (1<<j)) x ^= A[j];
31           if(!table.count(x) || bitcount(table[x]) < bitcount(i)) table[x] = i;
32           //table[x]保存的是xor值为x的,bitcount尽量大的子集
33     }
34 
35     int ans = 0;
36     REP(i,1<<n2){//枚举后n2个字符串的所有情况
37          int x = 0;
38          REP(j,n2) if(i & (1<<j) ) x ^= A[j + n1];
39          if(table.count(x) && bitcount(ans) < bitcount(table[x]) + bitcount(i)) ans = (i<<n1)^table[x];
40     }
41 
42     printf("%d\n",bitcount(ans));
43     REP(i,n) if(ans & (1<<i)) printf("%d ", i+1);
44     puts("");
45 }
46 
47 int main(){
48 //    freopen("in.txt","r",stdin);
49     while(scanf("%d",&n)>0&&n)
50         solve();
51     return 0;
52 }
View Code

 



UVALive - 2965 Jurassic Remains,布布扣,bubuko.com

UVALive - 2965 Jurassic Remains

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/zyx1314/p/3759526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!