题解

在一个字符串中,每个字符出现的次数本身是无关紧要的,重要的只是这些次数的奇偶性,因此想到用一个二进制的位表示一个字母($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. 『开源』设置系统 主音量(0~100 静音) VolumeHelper 兼容 Xp Win7 .Net 20 AnyCPU

    背景: 近来的生活一团乱麻,没心态写高大上的代码,于是就着手 写了几个 辅助类. 在整理 InkFx.Utils 时,发现有几个 辅助类 只写了定义,没有实现函数体,于是就 花了1天时间 完善了一下. ...

  2. 105&250-高级软件工程2017第3次作业

    小组成员 2017282110250 王婷婷 2017202110105 张芷祎 github地址 https://github.com/setezzy/Calculator_GUI PSP PSP2 ...

  3. python 异步协程

    """A very simple co-routine scheduler. Note: this is written to favour simple code ov ...

  4. python 之反射

    通过字符串的形式导入模块 通过字符串的形式,去模块中寻找制定的函数,并执行getattr(模块名,函数名,默认值) 通过字符串的形式,去模块中设置东西setattr(模块名,函数名/变量名,lambd ...

  5. C实现单链表

    typedef int DataType; typedef struct ListNode { DataType data; struct ListNode* next; }ListNode; //初 ...

  6. django处理cookie的机制

    title: django处理cookie的机制 tags: djaogo, cookie, session grammar_cjkRuby: true --- cookie的意义 在多数日常使用的网 ...

  7. Class-Based-View(CBV)

    我们都知道,Python是一个面向对象的编程语言,如果只用函数来开发,有很多面向对象的优点就错失了(继承.封装.多态).所以Django在后来加入了Class-Based-View.可以让我们用类写V ...

  8. myeclipse的导航器

    在myeclipse的导航器下面可以看到编译后的文件目录结构 如何打开导航器试图呢? 窗口->显示视图->导航器 windows->show view->Navigator 这 ...

  9. kubernetes进阶(03)kubernetes的namespace

    服务发现与负载均衡Kubernetes在设计之初就充分考虑了针对容器的服务发现与负载均衡机制,提供了Service资源,并通过kube-proxy配合cloud provider来适应不同的应用场景. ...

  10. Docker学习笔记 - Docker数据卷的备份和还原

    学习目标: 备份数据卷 还原数据卷 # 通过容器备份数据卷容器中的数据卷 docker run --volumes-from dvt5 -v ~/backup:/backup --name dvt10 ...