[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
这是我做的第一道状态压缩的题目,而且我自己居然看懂了,理解得还算透彻. 题意:给出若干个大写字母组成的字符串,然后选取尽量多的字符串使得这些字母出现偶数次. 最朴素的想法,穷举法:每个字符串只有选和不 ...
随机推荐
- 2017-2018-1 Java演绎法 第二周 作业
团队任务:讨论Android上的游戏软件 参考现代软件工程 第一章 [概论]练习与讨论: 软件有很多种,也有各种分类办法,本次团队任务是讨论选取Android上的一个游戏软件,考虑到每位组员接触的游戏 ...
- 需求分析&原型设计
需求分析&原型设计 需求分析 访问软件项目真实用户 首先本项目的用户是这个需要做简单四则运算的用户(我们团队通过对家里有三四年级小学生(需要做简单四则运算)的简单采访):反映了几个主要的问题: ...
- Numpy - 多维数组(上)
一.实验说明 numpy 包为 Python 提供了高性能的向量,矩阵以及高阶数据结构.由于它们是由 C 和 Fortran 实现的,所以在操作向量与矩阵时性能非常优越. 1. 环境登录 无需密码自动 ...
- Swift -欢迎界面1页, 延长启动图片的显示时间(LaunchImage)
转自:http://www.hangge.com/blog/cache/detail_1238.html http://www.hangge.com/blog/cache/detail_672.htm ...
- XML使用练习
#!/usr/bin/env python # -*- coding:utf-8 -*- import requests from xml.etree import ElementTree as ET ...
- 【iOS】跳转到设置页面
iOS8.0以后有效 定位服务 定位服务有很多APP都有,如果用户关闭了定位,那么,我们在APP里面可以提示用户打开定位服务.点击到设置界面设置,直接跳到定位服务设置界面.代码如下: 1 2 3 4 ...
- BEM 中文翻译
BEM 原文请看 getBEM Introduction(介绍) Block 独立实体,独立的意义 Examples:header, container, menu, checkbox, input ...
- C# 客户端程序调用外部程序的三种实现
简介 我们用C#来开发客户端程序的时候,总会不可避免的需要调用外部程序或者访问网站,本篇博客介绍了三种调用外部应用的方法,供参考 实现 第一种是利用shell32.dll,实现ShellExecute ...
- spring mvc跨域(ajax post json)--filter方案
@RequestMapping(value = "/login.do",method = RequestMethod.POST) public Message login(Http ...
- Codeforces Round #426 (Div. 2)
http://codeforces.com/contest/834 A. The Useless Toy 题意: <,>,^,v这4个箭头符号,每一个都可以通过其他及其本身逆时针或者顺时针 ...
