UVa 1326 - Jurassic Remains(枚举子集+中途相遇法)
训练指南p.59
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map> using namespace std; const int MAXN = ; int N;
int A[MAXN];
char str[];
map<int , int> table; int bitcount( int x )
{
int res = ;
while ( x )
{
if ( x & ) ++res;
x >>= ;
}
return res;
} int main()
{
while ( scanf( "%d", &N ) == )
{
for ( int i = ; i < N; ++i )
{
scanf( "%s", str );
A[i] = ;
for ( int j = ; str[j]; ++j )
A[i] |= ( << ( str[j] - 'A' ) );
} table.clear(); int n1 = N / , n2 = N - n1;
for ( int i = ; i < ( << n1 ); ++i )
{
int tmp = ;
for ( int j = ; j < n1; ++j )
if ( i & ( << j ) )
tmp ^= A[j];
if ( !table.count(tmp) || bitcount(i) > bitcount( table[tmp] ) ) table[tmp] = i;
} int ans = ;
for ( int i = ; i < ( << n2 ); ++i )
{
int tmp = ;
for ( int j = ; j < n2; ++j )
if ( i & ( << j ) ) tmp ^= A[ n1 + j ];
if ( table.count(tmp) && bitcount(ans) < bitcount(i) + bitcount(table[tmp]) )
ans = ( i << n1 ) | table[tmp];
} printf("%d\n", bitcount(ans) );
bool first = false;
for ( int i = ; i < N; ++i )
if ( ans & ( << i ) )
{
if ( first ) putchar(' ');
printf("%d", i + );
first = true;
}
puts("");
}
return ;
}
UVa 1326 - Jurassic Remains(枚举子集+中途相遇法)的更多相关文章
- uva 6757 Cup of Cowards(中途相遇法,貌似)
uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...
- uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)
用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...
- POJ 1903 & ZOJ 2469 & UVA 1326 Jurassic Remains (部分枚举)
题意:给定n个只有大写字母组成的字符串,选取尽可能多的字符串,使得这些字符串中每个字母的个数都是偶数.n<=24 思路:直接枚举每个字符串的选或不选,复杂度是O(2^n).其实还有更简便的方法. ...
- [UVa 1326]Jurassic Remains
题解 在一个字符串中,每个字符出现的次数本身是无关紧要的,重要的只是这些次数的奇偶性,因此想到用一个二进制的位表示一个字母($1$表示出现奇数次,$0$表示出现偶数次).比如样例的$6$个数,写成二进 ...
- UVA - 1608 Non-boring sequences (分治,中途相遇法)
如果一个序列中是否存在一段连续子序列中的每个元素在该子序列中都出现了至少两次,那么这个序列是无聊的,反正则不无聊.给你一个长度为n(n<=200000)的序列,判断这个序列是否无聊. 稀里糊涂A ...
- LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...
- 【UVALive】2965 Jurassic Remains(中途相遇法)
题目 传送门:QWQ 分析 太喵了~~~~~ 还有中途相遇法这种东西的. 嗯 以后可以优化一些暴力 详情左转蓝书P58 (但可能我OI生涯中都遇不到正解是这个的题把...... 代码 #include ...
- 紫书 习题 8-16 UVa 1618 (中途相遇法)
暴力n的四次方, 然而可以用中途相遇法的思想, 分左边两个数和右边两个数来判断, 最后合起来判断. 一边是n平方logn, 合起来是n平方logn(枚举n平方, 二分logn) (1)两种比较方式是相 ...
- 【uva 1152】4 Values Whose Sum is Zero(算法效率--中途相遇法+Hash或STL库)
题意:给定4个N元素几个A,B,C,D,要求分别从中选取一个元素a,b,c,d使得a+b+c+d=0.问有多少种选法.(N≤4000,D≤2^28) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...
随机推荐
- 安装gcc,g++
安装gcc.g++ sudo apt-get install make gcc g++ 查看g++版本 g++ --version
- Linux文件服务器实战(虚拟用户)
vsftpd基于系统用户访问ftp服务器,系统用户越多越不利于管理,不利于系统安全,这样就以vsftp虚拟防护的方式来解决. 虚拟用户没有实际的真实系统用户,,而是通过映射到其中一个真实用户以及设置相 ...
- [异常笔记] spring cloud 服务消费者启动-2018040501
一.异常信息: Error starting ApplicationContext. To display the auto-configuration report re-run your appl ...
- python实现排序之冒泡排序
冒泡排序:是将一串无需的数字,排列成有序的.通过相邻的两个数作比较,大的往后移,经过反复的比较,最后得出一串有序的数列. 那么用代码该如何实现? 其实这个问题的思路就是判断每相邻的两个数,进行大小比较 ...
- 时间轮算法的定时器(Delphi)
源码下载 http://files.cnblogs.com/lwm8246/uTimeWheel.rar D7,XE2 编译测试OK //时间轮算法的定时器 //-- : QQ unit uTimeW ...
- 神经网络的训练和测试 python
承接上一节,神经网络需要训练,那么训练集来自哪?测试的数据又来自哪? <python神经网络编程>一书给出了训练集,识别图片中的数字.测试集的链接如下: https://raw.githu ...
- JAVA-数组或集合
哈哈,今天我们来讲解一下有关于一些数组 或者是集合的知识点 1.ArrayList,LinkedList,Vector的区别 ArrayList,LinkedList,Vector都是实现List接口 ...
- Still unable to dial persistent://blog.csdn.net:80 after 3 attempts
动不动电脑有些网站打不开了,还报错: Still unable to dial persistent://blog.csdn.net:80 after 3 attempts 为什么呢? 是dns坏了? ...
- pdo事务
$pdo->beginTransaction() $pdo->commit() $pdo->rollback();
- webstorm git提交不成功的
git pull git pull origin master git pull origin master --allow-unrelated-histories