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) 解法:首先我们从最直接最暴力的方法开始思考:四重循 ...
随机推荐
- maven没有servlet(创建servlet后报错)
maven不能创建servlet 解决方案 方案一 在项目的iml进行指定根目录 <sourceRoots> <root url="file://$MODULE_DIR$/ ...
- PRmakefile文件
Ubuntu下的makefile: # /******************************************************************************* ...
- 斯坦福大学3D模型库-The Stanford 3D Scanning Repository
这里总是让人感觉有差距的地方!!! http://graphics.stanford.edu/data/3Dscanrep/
- git 提交 src refspec master does not match any
git init 产生的目录解释 error: src refspec master does not match any. 引起该错误的原因是,目录中没有文件,空目录是不能提交上去的 error ...
- POJ2409 Let it Bead(Polya定理)
Let it Bead Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6443 Accepted: 4315 Descr ...
- spring-mybatis整合项目 异常处理2
org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/imooc ...
- 基于mybatis设计简单信息管理系统---jsp页面
1.在设计编辑界面的时候需要有一个下拉的列表页,想要他指定到指定的值: <select id="categoryId" name="categoryId" ...
- 关于IT人的一些消遣区
https://www.csdn.net/http://www.51cto.com/http://bestcbooks.com/http://www.jobbole.com/http://www.co ...
- ethereum(以太坊)(四)--值传递与引用传递
contract Person { string public _name; function Person() { _name = "liyuechun"; } function ...
- 什么是web语义化?
Web语义化:是指使用语义恰当的标签,使页面有良好的结构,页面元素更有含义,能够让人和搜索引擎都容易理解.使团队项目的可持续运作及维护,去掉样式后页面呈现清晰的结构. 例如:<table> ...