UVa LA 2965 - Jurassic Remains 中间相遇,状态简化 难度: 2
题目
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=966
题意
n个大写字母串(n <= 24),问最多取多少个,使得所有字符出现的次数都是偶数。
思路
如刘书
1. 由于最多出现26个字母,而且字母在字符串中出现的次数本身不重要,只要记录奇偶性,所以可以将这些字符串转化为01串便于存储。
2. 问题转化为最多取多少个01串,使得其异或结果为0
3. 这样就可以用2^24来枚举,但这样状态还是太多了
4. 问题可以转化为前n/2个字符串子集组成的异或结果与后n/2个字符串子集组成的异或结果相同的情况下,最多取多少个字符串。
感想
1. 一开始忘了字符串本身可能存在重复的字符串
2. 之后忘了程序会修改pos
代码
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
const int MAXN = ;
int n;
int a[MAXN];
int staStack[ << MAXN];
int posStack[ << MAXN]; int bone2sta(char * str) {
int sta = ;
for (char * p = str; *p != ; p++) {
sta ^= << (*p - 'A');
}
return sta;
} int getDigitCnt(int x) {
int ans = ;
while (x > ) {
x -= (x & -x);
ans++;
}
return ans;
} int main() {
#ifdef LOCAL_DEBUG
freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
//int T;
// scanf("%d", &T); for (int ti = ;scanf("%d", &n) == && n; ti++) {
for (int i = ; i < n; i++) {
char buff[];
scanf("%s", buff);
a[i] = bone2sta(buff);
}
int half_n = n / ;
map<int, int> sta2pos;
sta2pos[] = ;
int staCnt = ;
staStack[staCnt++] = ;
for (int i = ; i < half_n; i++) {
for (int j = staCnt - ; j >= ; j--) {
posStack[j] = sta2pos[staStack[j]];
}
for (int j = staCnt - ; j >= ; j--) {
int sta = staStack[j];
int pos = posStack[j];
int newSta = sta ^ a[i];
int newPos = pos | ( << i);
if (sta2pos.count(newSta) == ) {
sta2pos[newSta] = newPos;
staStack[staCnt++] = newSta; }
else {
int oldDigitCnt = getDigitCnt(sta2pos[newSta]);
int newDigitCnt = getDigitCnt(pos) + ;
if(newDigitCnt > oldDigitCnt)sta2pos[newSta] = newPos;
}
}
}
map<int, int> othersta2pos;
othersta2pos[] = ;
staCnt = ;
staStack[staCnt++] = ;
for (int i = half_n; i < n; i++) {
for (int j = staCnt - ; j >= ; j--) {
posStack[j] = othersta2pos[staStack[j]];
}
for (int j = staCnt - ; j >= ; j--) {
int sta = staStack[j];
int pos = posStack[j];
int newSta = sta ^ a[i];
int newPos = pos | ( << i);
if (othersta2pos.count(newSta) == ) {
othersta2pos[newSta] = newPos;
staStack[staCnt++] = newSta; }
else {
int oldDigitCnt = getDigitCnt(othersta2pos[newSta]);
int newDigitCnt = getDigitCnt(pos) + ;
if (newDigitCnt > oldDigitCnt)othersta2pos[newSta] = newPos;
}
}
}
int ans = , ansPos = ;
for (int j = ; j < staCnt; j++) {
int sta = staStack[j];
if (sta2pos.count(sta) != ) {
int digitCnt = getDigitCnt(sta2pos[sta]) + getDigitCnt(othersta2pos[sta]);
if (digitCnt > ans) {
ans = digitCnt;
ansPos = sta2pos[sta] | othersta2pos[sta];
}
}
}
printf("%d\n", ans);
for (int i = ; i < n; i++) {
if (ansPos & ( << i)) {
printf("%d ", i + );
}
}
puts("");
} return ;
}
UVa LA 2965 - Jurassic Remains 中间相遇,状态简化 难度: 2的更多相关文章
- LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...
- LA 2965 Jurassic Remains
这是我做的第一道状态压缩的题目,而且我自己居然看懂了,理解得还算透彻. 题意:给出若干个大写字母组成的字符串,然后选取尽量多的字符串使得这些字母出现偶数次. 最朴素的想法,穷举法:每个字符串只有选和不 ...
- UVALive - 2965 Jurassic Remains (LA)
Jurassic Remains Time Limit: 18000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Sub ...
- 【UVALive】2965 Jurassic Remains(中途相遇法)
题目 传送门:QWQ 分析 太喵了~~~~~ 还有中途相遇法这种东西的. 嗯 以后可以优化一些暴力 详情左转蓝书P58 (但可能我OI生涯中都遇不到正解是这个的题把...... 代码 #include ...
- UVaLive 2965 Jurassic Remains (状态压缩)
题意:给定 n 个大写字母组成的字符串,选择尽量多的串,使得大写字母都能出现偶数次. 析:由于n比较小,我们可以枚举前n/2的所有组合,然后再从后面查找. 代码如下: #pragma comment( ...
- uvalive 2965 Jurassic Remains
https://vjudge.net/problem/UVALive-2965 题意: 给出若干个由大写字母组成的字符串,要求选出尽量多的字符串,使得每个大写字母出现的次数是偶数. 思路: 如果说我们 ...
- 【中途相遇+二进制】【NEERC 2003】Jurassic Remains
例题25 侏罗纪(Jurassic Remains, NEERC 2003, LA 2965) 给定n个大写字母组成的字符串.选择尽量多的串,使得每个大写字母都能出现偶数次. [输入格式] 输入包含 ...
- Meeting-in-the-Middle (LA 2965)
Meeting-in-the-Middle,又称“中途相遇法”.准确地说,它只是一种策略. 顺便说一下,这个算法真的很冷门! 结合这道题来讨论一下吧:LA 2965.ε(┬┬﹏┬┬)3 因为博主的英文 ...
- UVA LA 7146 2014上海亚洲赛(贪心)
option=com_onlinejudge&Itemid=8&page=show_problem&category=648&problem=5158&mosm ...
随机推荐
- eclipse中配置maven和创建第一个 Spring Boot Application
关于Maven的下载.环境变量的配置自行百度,今天记录一下在Eclipse中配置Maven的操作: mvn -v 出现上图说明maven和jdk的要求都达到了(jdk要8.0及以上的版本) 然后在ec ...
- Android 虹软免费人脸识别App
人脸识别+本机Web后端 人脸sdk采用虹软sdk,本机web采用AndServer:上传姓名+人脸图片即可实现注册源码地址:https://github.com/joetang1989/ArcFac ...
- [C#]读取不同版本的excel文件的方法
--------------------------------2007及以上的版本-------------------------------- 测试如下: //DataInterface.Met ...
- Python3 - MySQL适配器 PyMySQL
本文我们为大家介绍 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查. 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一 ...
- 第 7 章 多主机管理 - 046 - 创建 Machine
创建 Machine Machine 就是运行 docker daemon 的主机. “创建 Machine” 指的就是在 host 上安装和部署 docker. 创建第一个 machine: hos ...
- missing seperator error when [make all]
https://stackoverflow.com/questions/16931770/makefile4-missing-separator-stop makefile has a very st ...
- 雷林鹏分享:jQuery EasyUI 树形菜单 - 树形网格惰性加载节点
jQuery EasyUI 树形菜单 - 树形网格惰性加载节点 有时我们已经得到充分的分层树形网格(TreeGrid)的数据. 我们还想让树形网格(TreeGrid)按层次惰性加载节点. 首先,只加载 ...
- Kaggle泰坦尼克数据科学解决方案
原文地址如下: https://www.kaggle.com/startupsci/titanic-data-science-solutions --------------------------- ...
- NexT 个性化设置
NexT 主题添加分类页面 新建页面 在本地使用终端 cd 到 blog 文件夹下,执行如下命令: $ cd Documents/blog $ hexo new page categories 设置页 ...
- 如何使用mysql profiling功能分析单条查询语句
Mysql从5.1版本开始引入show profile来剖析单条语句功能 一. 查看是否支持这个功能 yes表示支持 mysql> show variables like 'have_profi ...