[UVa 1326]Jurassic Remains
题解
在一个字符串中,每个字符出现的次数本身是无关紧要的,重要的只是这些次数的奇偶性,因此想到用一个二进制的位表示一个字母($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的更多相关文章
- POJ 1903 & ZOJ 2469 & UVA 1326 Jurassic Remains (部分枚举)
题意:给定n个只有大写字母组成的字符串,选取尽可能多的字符串,使得这些字符串中每个字母的个数都是偶数.n<=24 思路:直接枚举每个字符串的选或不选,复杂度是O(2^n).其实还有更简便的方法. ...
- UVa 1326 - Jurassic Remains(枚举子集+中途相遇法)
训练指南p.59 #include <cstdio> #include <cstring> #include <cstdlib> #include <map& ...
- UVALive - 2965 Jurassic Remains (LA)
Jurassic Remains Time Limit: 18000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Sub ...
- LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...
- 【中途相遇+二进制】【NEERC 2003】Jurassic Remains
例题25 侏罗纪(Jurassic Remains, NEERC 2003, LA 2965) 给定n个大写字母组成的字符串.选择尽量多的串,使得每个大写字母都能出现偶数次. [输入格式] 输入包含 ...
- LA2965 Jurassic Remains
Jurassic Remains https://vjudge.net/problem/UVALive-2965 Paleontologists in Siberia have recently fo ...
- UVa LA 2965 - Jurassic Remains 中间相遇,状态简化 难度: 2
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 【数论】UVa 10586 - Polynomial Remains
Problem F: Polynomial Remains Given the polynomial a(x) = an xn + ... + a1 x + a0, compute the remai ...
- LA 2965 Jurassic Remains
这是我做的第一道状态压缩的题目,而且我自己居然看懂了,理解得还算透彻. 题意:给出若干个大写字母组成的字符串,然后选取尽量多的字符串使得这些字母出现偶数次. 最朴素的想法,穷举法:每个字符串只有选和不 ...
随机推荐
- 分区表SQL调优/优化(Tuning)时容易“被欺骗”的场景之一
近几天没有用户找到,除了看看书,就是上网浏览点东西,好不惬意.可惜好景不长,正在享受悠闲惬意的日子时,一个用户的工作人员QQ找到我,说他们在统计一些数据,但一个SQL特别慢,或者说就从来没出过数据,我 ...
- Duplicate column name 'vocabulary'
创建一个视图: 报错:Duplicate column name 'vocabulary' 意思是视图select的列名重复了,取别名 改成这样就ok了
- JDK1.8源码(六)——java.util.LinkedList 类
上一篇博客我们介绍了List集合的一种典型实现 ArrayList,我们知道 ArrayList 是由数组构成的,本篇博客我们介绍 List 集合的另一种典型实现 LinkedList,这是一个有链表 ...
- C语言博客作业--字符数组-陈张鑫
一.PTA实验作业(4分) 题目1:7-5 查验身份证 1. 本题PTA提交列表(要提交列表,不是结果) 2. 设计思路(伪代码或流程图) 定义变量身份证个数n,合法个数count=0,flag=0, ...
- highcharts 具体参数详解
<script type="text/javascript" src="js/jquery.min.js"></script> < ...
- python性能分析--cProfile
Python标准库中提供了三种用来分析程序性能的模块,分别是cProfile, profile和hotshot,另外还有一个辅助模块stats.这些模块提供了对Python程序的确定性分析功能,同时也 ...
- linux的链接工具secure设置字体大小和颜色
- 第二章 Idea搭建maven
第二章 Idea搭建maven 1.配置Maven的环境变量 a.首先我们去maven官网下载Maven程序,解压到安装目录,如图所示: b.配置M2_HOME(MAVEN_HOME)的环境变量,然后 ...
- Django REST framework+Vue 打造生鲜超市(二)
三.Models设计 3.1.项目初始化 (1)进虚拟环境下安装 django2.0.2 djangorestframework和相关依赖mark,filter pillow 图片处理 pip in ...
- Python Tornado初学笔记之表单与模板(一)
Tornado中的表单和HTML5中的表单具有相同的用途,同样是用于内容的填写.只是不同的是Tornado中的表单需要传入到后台,然后通过后台进行对模板填充. 模板:是一个允许嵌入Python代码片段 ...