LA2965 n个数中选出最多个数异或和为0
intput
n 1<=n<=24
n串只有大写字母的字符串
output
选出最多个字符串且每个大写字母出现的次数为偶数
第一行输出个数x
第二行输出x个字符串的下标
做法:将每个字符串转化为一个26bit数,1为奇数个大写字母,0为偶数个,则转化为找出最多个数异或和为0,直接枚举为O((2^n)*n),但只有a^a=0,所以将n个数分为两半(中途相遇法),复杂度降为O((2^(n/2))logn)
注意:异或和左半为0和右半为0的要特判,且右半要全部判完,因为可能出现2+3<5+1
- #include <cstdio>
- #include <queue>
- #include <cstring>
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
- #include <vector>
- #include <map>
- #include <set>
- #include <ctime>
- #include <cmath>
- #define MAX 100000
- using namespace std;
- struct node
- {
- int mark,n;
- node operator+(const node&b)const
- {
- return (node){mark|b.mark,n+b.n};
- }
- bool operator<(const node&a)const
- {
- return n<a.n;
- }
- };
- int a[],c[],n,ch,num;
- char s[];
- node maxn;
- map<int,node>q;
- map<int,node>::iterator qsum;
- void dfs(int x,int mark,int sum,int nn)
- {
- if(x==n>>)
- {
- q.insert(make_pair(sum,(node){mark,nn}));
- if(sum==) maxn=max(maxn,(node){mark,nn});
- return;
- }
- sum^=a[x];
- nn++;
- mark|=<<x;
- dfs(x+,mark,sum,nn);
- sum^=a[x];
- nn--;
- mark&=~(<<x);
- dfs(x+,mark,sum,nn);
- }
- void find(int x,int mark,int sum,int nn)
- {
- if(x==n)
- {
- qsum=q.find(sum);
- if(qsum!=q.end()) maxn=max(maxn,qsum->second+(node){mark,nn});
- if(sum==) maxn=max(maxn,(node){mark,nn});
- return;
- }
- sum^=a[x];
- nn++;
- mark|=<<x;
- find(x+,mark,sum,nn);
- sum^=a[x];
- nn--;
- mark&=~(<<x);
- find(x+,mark,sum,nn);
- }
- int main()
- {
- freopen("/home/user/桌面/in","r",stdin);
- while(scanf("%d%*c",&n)==)
- {
- memset(a,,sizeof(a));
- for(int i=;i<n;i++)
- {
- memset(c,,sizeof(c));
- scanf("%s",s);
- for(int j=;s[j];j++) c[s[j]-'A']++;
- for(int j=;j<;j++) if(c[j]&) a[i]|=<<j;
- }
- if(n==)
- {
- if(a[]) puts("0\n");
- else puts("1\n1");
- continue;
- }
- q.clear();
- maxn=(node){,};
- dfs(,,,);
- find(n>>,,,);
- if(maxn.n)
- {
- printf("%d\n",maxn.n);
- for(int i=,j=;i<n;i++)
- {
- if(maxn.mark&(<<i))
- {
- if(j) putchar(' ');
- printf("%d",i+);
- j=;
- }
- }
- printf("\n");
- }
- else puts("0\n");
- }
- //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
- return ;
- }
LA2965 n个数中选出最多个数异或和为0的更多相关文章
- 一道经典的面试题:如何从N个数中选出最大(小)的n个数
转载:https://zhidao.baidu.com/question/1893908497885440140.html 这个问题我前前后后考虑了有快一年了,也和不少人讨论过.据我得到的消息,Goo ...
- JAVA 递归实现从n个数中选取m个数的所有组合
这周Java课程有个小作业:Java递归实现从n个数中选取m个数的所有组合 代码如下: //其中 n 取 1,2,3,4,5 五个数, m 取 3 package javaText; public c ...
- C++从多n个数中选取m个数的组合
//start 是从哪个开始取, picked代表已经取了多少个数 //process和data是全局变量数组 //语言说明比较难,我举个例子吧 //从[ 1, 2, 3, 4 ]中选取 2 个数 / ...
- Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5646 Accepted: 1226 Description In an ...
- 找出n个数中重复最多的10个数
题目很清晰,直接上python代码 import pandas as pd import copy class BenchMark: def __init__(self): self.MIN = 10 ...
- hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值
pog loves szh II Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax
275. To xor or not to xor The sequence of non-negative integers A1, A2, ..., AN is given. You are ...
- 从1到n整数中1的个数
[问题]求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...
- LA2965侏罗纪(异或和为0的最大数字个数)
题意: 给你n个字符串,让你在里面找到一个字符串集合使得这些字符串中所有的字母出现的次数和为偶数,输出集合的最大个数,和ASCII最小的解. 思路: 考虑到每个字符串中所有的字 ...
随机推荐
- @Transactional注解详解
默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exception("...");不会回滚 ...
- CF 602C The Two Routes(dij+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- 用Bash脚本将Linux普通用户添加为系统管理员
将Linux普通用户添加为系统管理员在Gnome或KDE这样强大与完善的桌面环境下是非常简单的事情,一般来说在用户设置的对话框里就直接有相应选项.不过,出于简洁与高效的风格,自己目前并未使用这些高端但 ...
- mnesia
1.模式创建 mnesia:create_schema([node()|nodes()]).集群的节点之间创建模式 2.启动和停止 application:start(mnesia). applica ...
- 将json文件转换为字符串
//从给定位置读取Json文件 public String readJson(String path){ //从给定位置获取文件 File file = new ...
- Asynchronous JS: Callbacks, Listeners, Control Flow Libs and Promises
非常好的文章,讲javascript 的异步编程的. ------------------------------------------------------------------------- ...
- dom入门
当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). //显示,改变html内容 document.getElementById("p1" ...
- URL scheme添加以及查找方式
2.1.1 添加URL Types URL Scheme是通过系统找到并跳转对应app的一类设置,通过向项目中的info.plist文件中加入URL types可使用第三方平台所注册的appkey信 ...
- IIS:错误: 无法提交配置更改,因为文件已在磁盘上更改
文件名: \\?\C:\Windows\system32\inetsrv\config\applicationHost.config 错误: 无法提交配置更改,因为文件已在磁盘上更改 通过 Micro ...
- Sikuli:创新的图形化编程技术
Sikuli是一种使用截图进行UI自动化测试的技术.Sikuli包括sikul脚本,基于Jython的API以及sikuli IDE.Sikuli可以实现任何你可以在显示器上看到ui对象的自动化,你可 ...