题解

在一个字符串中,每个字符出现的次数本身是无关紧要的,重要的只是这些次数的奇偶性,因此想到用一个二进制的位表示一个字母($1$表示出现奇数次,$0$表示出现偶数次)。比如样例的$6$个数,写成二进制后如图所示。

此时,问题转化为求尽量多的数,使得它们的$xor$值为$0$。

最容易想到的方法是直接穷举,时间复杂度为$O(2^n)$,有些偏大。注意到$xor$值为$0$的两个整数必须完全相等,我们可以把字符串分成两个部分:首先计算前$n \over 2$个字符串所能得到的所有$xor$值,并将其保存到一个映射$S$($xor$值->前$n \over 2$个字符串的一个子集)中;然后枚举后$n \over 2$个字符串所能得到的所有$xor$值,并每次都在$S$中查找。

如果映射用$STL$的$map$实现,总时间复杂度为$O(2^{n \over 2}log_2 n)$,即$O(1.44^n log_2 n)$,比第一种方法好了很多。

 //It is made by Awson on 2017.9.20
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define lowbit(x) ((x)&(-(x)))
#define LL long long
using namespace std; int n;
char ch[];
int a[];
map<int, int> mp; int bitcount(int x) {
int cnt = ;
for (; x; x -= lowbit(x)) cnt++;
return cnt;
} void work() {
for (int i = ; i < n; i++) {
scanf("%s", ch);
a[i] = ;
for (int j = ; j < strlen(ch); j++) a[i] ^= <<ch[j]-'A';
}
mp.clear();
int mid = n/;
int lim = <<mid;
for (int i = ; i < lim; i++) {
int tmp = ;
for (int j = ; j < mid; j++)
if (i&(<<j)) tmp ^= a[j];
if (!mp.count(tmp) || bitcount(mp[tmp]) < bitcount(i))
mp[tmp] = i;
}
int ans = ;
lim = <<(n-mid);
for (int i = ; i < lim; i++) {
int tmp = ;
for (int j = mid; j < n; j++)
if (i&(<<j-mid)) tmp ^= a[j];
if (mp.count(tmp) && bitcount(ans) < bitcount(mp[tmp])+bitcount(i)) ans = (i<<mid)|mp[tmp];
}
printf("%d\n", bitcount(ans));
for (int i = ; i < n; i++)
if (ans&(<<i)) printf("%d ", i+);
putchar('\n');
}
int main() {
while (~scanf("%d", &n))
work();
return ;
}

[UVa 1326]Jurassic Remains的更多相关文章

  1. POJ 1903 & ZOJ 2469 & UVA 1326 Jurassic Remains (部分枚举)

    题意:给定n个只有大写字母组成的字符串,选取尽可能多的字符串,使得这些字符串中每个字母的个数都是偶数.n<=24 思路:直接枚举每个字符串的选或不选,复杂度是O(2^n).其实还有更简便的方法. ...

  2. UVa 1326 - Jurassic Remains(枚举子集+中途相遇法)

    训练指南p.59 #include <cstdio> #include <cstring> #include <cstdlib> #include <map& ...

  3. UVALive - 2965 Jurassic Remains (LA)

    Jurassic Remains Time Limit: 18000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Sub ...

  4. LA 2965 Jurassic Remains (中途相遇法)

    Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...

  5. 【中途相遇+二进制】【NEERC 2003】Jurassic Remains

    例题25  侏罗纪(Jurassic Remains, NEERC 2003, LA 2965) 给定n个大写字母组成的字符串.选择尽量多的串,使得每个大写字母都能出现偶数次. [输入格式] 输入包含 ...

  6. LA2965 Jurassic Remains

    Jurassic Remains https://vjudge.net/problem/UVALive-2965 Paleontologists in Siberia have recently fo ...

  7. UVa LA 2965 - Jurassic Remains 中间相遇,状态简化 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  8. 【数论】UVa 10586 - Polynomial Remains

    Problem F: Polynomial Remains Given the polynomial a(x) = an xn + ... + a1 x + a0, compute the remai ...

  9. LA 2965 Jurassic Remains

    这是我做的第一道状态压缩的题目,而且我自己居然看懂了,理解得还算透彻. 题意:给出若干个大写字母组成的字符串,然后选取尽量多的字符串使得这些字母出现偶数次. 最朴素的想法,穷举法:每个字符串只有选和不 ...

随机推荐

  1. CSS的盒子模型有哪些,区别是什么

    1)盒模型: 内容(content).填充(padding).边界(margin). 边框(border)   2)有两种, IE 盒子模型.标准 W3C 盒子模型:IE的content部分包含了 b ...

  2. 论C++的智能指针

    一.简介   参考这篇博客,并且根据<C++ Primer>中相关知识,我总结了C++关于智能指针方面的内容.   为了解决内存泄漏的问题,便出现了智能指针.STL提供的智能指针有:aut ...

  3. Flask 学习 十四 测试

    获取代码覆盖报告 安装代码覆盖工具 pip install coverage manage.py 覆盖检测 COV = None if os.environ.get('FLASK_COVERAGE') ...

  4. Python习题(第一课)

    想了想其他的太简单了,还是不放了,剩三题吧. 一.完美立方 编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组(a, b, c, d),使得a^3= b^3 + c^3 + d^3,其中 ...

  5. java 注解的实现机制

    一.什么是注解: 注解是标记,也可以理解成是一种应用在类.方法.参数.属性.构造器上的特殊修饰符.注解作用有以下三种: 第一种:生成文档,常用的有@param@return等. 第二种:替代配置文件的 ...

  6. 04_Linux目录文件操作命令1(mv ls cd...)_我的Linux之路

    上一节已经给大家讲了Linux的目录结构,相信大家已经对Linux的整个目录结构有所了解 现实中,服务器(包含Linux,Unix,windows server)一般都摆放在机房里,因为一个机房摆放了 ...

  7. SpringBoot应用的属性管理

    一.properties 配置文件 1.src/main/application.properties spring.profiles.active=dev spring.application.na ...

  8. python入门(2)python的安装

    python入门(2)python的安装 Python是跨平台的,可以运行在Windows.Mac和各种Linux/Unix系统上. 2.x还是3.x Python有两个版本,一个是2.x版,一个是3 ...

  9. HTML5的常用新特性你必须知道

    HTML5的常用新特性你必须知道 1 新的 声明 HTML 有多个不同的版本,只有完全明白页面中使用的确切 HTML 版本,浏览器才能完全正确地显示出 HTML 页面.这就是 的用处. 不是 HTML ...

  10. CTF中常见密码题解密网站总结

    0x00.综合 网站中包含大多编码的解码. http://web2hack.org/xssee/ https://www.sojson.com/ http://web.chacuo.net/ 0x01 ...