LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains
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 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 题目大意:侏罗纪。给定n个大写字母组成的字符串。选择尽量多的串,使得每个大写字母都能出现偶数次。 分析:在一个字符串中,每个字符出现的次数无关紧要,重要的是这些次数的奇偶性,因此想到用一个二进制的位表示一个字母(1表示出现奇数次,0表示出现偶数次)。
样例写成二进制后为
A B C D E F G H
1 1 0 1 0 0 0 0 A B D
0 0 0 0 1 0 1 0 E G
0 0 0 0 1 0 1 0 G E
1 1 0 0 1 0 0 0 A B E
1 0 1 0 0 0 0 0 A C
0 1 1 1 0 0 0 0 B C D 问题转化为求尽量多的数,使得它们的xor值为0
穷举法会超时。注意到xor值为0的两个整数必须完全相等,可以把字符串分成两个部分:首先计算前n/2个字符串所能得到的所有xor值,并将其保存到一个映射S(xor值→ 前n/2个字符串的一个子集)中;然后枚举后n/2个字符串所能得到的所有xor值,并每次都在S中查找。
如果映射用STL的map实现,总时间复杂度为O(2n/2logn),这种方法称为中途相遇法。 代码如下:
#include<cstdio>
#include<map>
using namespace std; const int maxn = ;
map<int,int> table; int bitcount(int x) { return x == ? : bitcount(x/) + (x&); } int main() {
int n, A[maxn];
char s[]; while(scanf("%d", &n) == && n) {
// 输入并计算每个字符串对应的位向量
for(int i = ; i < n; i++) {
scanf("%s", s);
A[i] = ;
for(int j = ; s[j] != '\0'; j++) A[i] ^= (<<(s[j]-'A'));
}
// 计算前n1个元素的所有子集的xor值
// table[x]保存的是xor值为x的,bitcount尽量大的子集
table.clear();
int n1 = n/, n2 = n-n1;
for(int i = ; i < (<<n1); i++) {
int x = ;
for(int j = ; j < n1; j++) if(i & (<<j)) x ^= A[j];
if(!table.count(x) || bitcount(table[x]) < bitcount(i)) table[x] = i;
}
// 枚举后n2个元素的所有子集,并在table中查找
int ans = ;
for(int i = ; i < (<<n2); i++) {
int x = ;
for(int j = ; j < n2; j++) if(i & (<<j)) x ^= A[n1+j];
if(table.count(x)&&bitcount(ans)<bitcount(table[x])+bitcount(i)) ans = (i<<n1)^table[x];
}
// 输出结果
printf("%d\n", bitcount(ans));
for(int i = ; i < n; i++) if(ans & (<<i)) printf("%d ", i+);
printf("\n");
}
return ;
}
LA 2965 Jurassic Remains (中途相遇法)的更多相关文章
- UVa LA 2965 - Jurassic Remains 中间相遇,状态简化 难度: 2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- LA 2965 Jurassic Remains
这是我做的第一道状态压缩的题目,而且我自己居然看懂了,理解得还算透彻. 题意:给出若干个大写字母组成的字符串,然后选取尽量多的字符串使得这些字母出现偶数次. 最朴素的想法,穷举法:每个字符串只有选和不 ...
- 【UVALive】2965 Jurassic Remains(中途相遇法)
题目 传送门:QWQ 分析 太喵了~~~~~ 还有中途相遇法这种东西的. 嗯 以后可以优化一些暴力 详情左转蓝书P58 (但可能我OI生涯中都遇不到正解是这个的题把...... 代码 #include ...
- LA 2965 中途相遇法
题目链接:https://vjudge.net/problem/UVALive-2965 题意: 有很多字符串(24),选出一些字符串,要求这些字符串的字母都是偶数次: 分析: 暴力2^24也很大了, ...
- UVALive - 2965 Jurassic Remains (LA)
Jurassic Remains Time Limit: 18000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Sub ...
- uva 6757 Cup of Cowards(中途相遇法,貌似)
uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...
- HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))
Difference Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 高效算法——J 中途相遇法,求和
---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
随机推荐
- web pack的使用事项
一开始把webpack想的很难,很复杂,哈哈. 其实写的好的东西真的是越简单,越简单的东西其实越难写. 按照How to一步一步基本就能理解,webpack怎么使用了, 后面就是如何帮程序一步一步找到 ...
- svn 如何解决冲突
项目中,往往不止你一人开发,多人开发,难免会有代码的冲突.彼此间谁也不能保证不会修改同个文件.如果修改了同个方法的内容.这时提交到svn是会提示代码冲突的. 当然,冲突是可控的,但不能避免.每次写代码 ...
- 【转】于request.getSession(true/false/null)的区别
http://blog.csdn.net/gaolinwu/article/details/7285783 关于request.getSession(true/false/null)的区别 一.需求原 ...
- 【51】编写new和delete时需固守常规
1.[50]讲了,有很多理由需要写个自定义的new/delete,自定义new/delete的时候,需要遵守一些规则. 2.循环申请,直到成功或者抛出异常,如下: void* operator new ...
- 《Java并发编程实战》第十五章 原子变量与非堵塞同步机制 读书笔记
一.锁的劣势 锁定后假设未释放.再次请求锁时会造成堵塞.多线程调度通常遇到堵塞会进行上下文切换,造成很多其它的开销. 在挂起与恢复线程等过程中存在着非常大的开销,而且通常存在着较长时间的中断. 锁可能 ...
- poj 1147 Binary codes
Binary codes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5647 Accepted: 2201 Desc ...
- POJ 3692 最大独立集
题意:有G个女生,B个男生,所有的女生都互相认识,所有的男生都互相认识,还有N对男女,他们互相认识. 问从中选出最多的人数,是的他们全部互相认识. 思路:这道题的构图很巧妙,对于他的补图构图,对于所有 ...
- Spring MVC page render时jsp中元素相对路径的解决办法
前段时间做了用Spring Security实现的登录和访问权限控制的功能,但是page render使用的是InternalResourceResolver,即在spring的servlet配置文件 ...
- leetcode第一刷_Jump Game
这个题事实上非常easy的,我一開始想复杂了,它没要求记录路径,事实上仅仅要看一下每一步之后所能延伸到的最远的位置就能够了,在这一个最远位置前面的那些位置,都是能够到达的,假设扫到了某个i,它大于当前 ...
- MVC - 学习总目录
MVC - 基础 MVC - HtmlHelper类 MVC - 路由 MVC - 身份验证 MVC - 模型验证 MVC - Ajax MVC - 布局