ACM: poj 1903

Jurassic Remains

Description
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

The first line of the input contains N -- the number of bones (1 <= N <= 24). Next N lines contain bones descriptions: each line contains a non-empty sequence of di#erent capital letters, representing labels marking the joints of the corresponding bone.

Output

On the first line of output print L -- the maximal possible number of bones that could be used to reassemble skeleton fragments. Then print on the next line L integer numbers in ascending order -- the bones to be used. Bones are numbered starting from one, as they are given in the input.

Sample Input

6

ABD EG GE ABE AC BCD

Sample Output

5

1 2 3 5 6

题意: 在给出n个序列中, 尽量选出多的字符串, 使得全部字母出现为偶数. 输出选择个数和
     选择字符串序号.

解题思路:(刘汝佳-训练指南的方法)
     1. 只关注每个字母出现的奇数和偶数的情况, 用0/1表示, 1: 表示出现奇数, 0: 出现偶数.
        记录每个字符串的每个字符出现的奇数偶数情况.
     2. 选择前n/2个字符串, 枚举出全部的所有可以组成的异或值, 接着找出剩下的字符串找出,
        跟前n/2个字符串中, 同样的异或值, 因为异或值一样表示这样组合起来全部的字母都是
        偶数.
     3. 这种策略叫: 中途相遇法.

代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
#define MAX 30

int n;
int a[MAX];
map myIndex;

int getBit(int x)
{
    return x == 0 ? 0 : getBit(x>>1)+(x&1);
}

int main()
{
//    freopen("input.txt", "r", stdin);
    while(scanf("%d", &n) != EOF)
    {
        myIndex.clear();
        char ch[1005];
        for(int i = 0; i < n; ++i)
        {
            scanf("%s", ch);
            a[i] = 0;
            for(int j = 0; ch[j] != '\0'; ++j)
                a[i] ^= (1<<(ch[j]-'A'));
        }
       
        int n1 = n/2, n2 = n-n1;
        for(int i = 0; i < (1<<n1); ++i)
         
            int x = 0;
            for(int j = 0; j < n1; ++j)
            {
                if(i&(1<<j))
                    x ^= a[j];
            }
            if(!myIndex.count(x) || getBit(myIndex[x]) < getBit(i))
                myIndex[x] = i; //异或值为x的尽量找i大, 因为字符串越多
        }
       
        int result = 0;
        for(int i = 0; i < (1<<n2); ++i)
        {
            int x = 0;
            for(int j = 0; j < n2; ++j)
            {
                if(i & (1<<j))
                    x ^= a[n1+j];
            }
            if(myIndex.count(x) && (getBit(result) < getBit(myIndex[x])+getBit(i)) )
                result = (i<<n1)^myIndex[x]; //i情况往前移n1位, 组合一个n位结果
        }
       
        bool flag = false;
        printf("%d\n", getBit(result));
        for(int i = 0; i < n; ++i)
        {
            if(result & (1<<i))
            {
                if(!flag)
                         
                    printf("%d", i+1);
                    flag = true;
                }
                else printf(" %d", i+1);
            }
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值