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最小的解. 思路: 考虑到每个字符串中所有的字 ...
随机推荐
- web工程师经常遇到的专业术语(待补充)
接口(API) SEO指的是将从各个方面对网站内容进行优化,方便搜索引擎检索.搜索引擎工作的原理就是对网站内容进行抓取,然后解析. 白帽SEO:合理的对网站内容进行优化进而增加搜索引擎友好度. 黑帽S ...
- submit 防止重复提交 --禁止提交
<form action="/apply/apply" method="POST" id="indentForm"> <p ...
- Python之迭代器&装饰器&生成器&正则
1.迭代器 迭代器是访问数据集合的一种方式,它只能从集合的第一个元素开始顺序访问,直到最后一个元素结束.类似于linux里的cat命令,只能挨行读取文本内容,不可以跳到中间或者尾部读取(不会把所有的数 ...
- 再谈Java方法传参那些事
把一个变量带进一个方法,该方法执行结束后,它的值有时会改变,有时不会改变.一开始会觉得--“好神奇呀”.当我们了解java内存分析的知识后,一切都是那么简单明了了--“哦,这么回事呀”.但是今天的上机 ...
- 判断一个数是否为2的n次幂
参考:http://bbs.csdn.net/topics/370058619 如题,如何判断一个整数是否是2的N次方,我能想到的方法有两个 1.一直除2,看最后是否等于1.(最笨的方法) 2.转换成 ...
- OpenGL多视口
#include <gl/glew.h> #include <gl/freeglut.h> #include <iostream> ; ; float rtri = ...
- kettle 连接Hadoop
http://wiki.pentaho.com/display/BAD/Additional+Configuration+for+YARN+Shims Copy *-site.xml Cluster ...
- 杭电20题 Human Gene Functions
Problem Description It is well known that a human gene can be considered as a sequence, consisting o ...
- Python基础篇-day3
主要内容:字典 集合 文件处理 字符编码 1.字典dict简介dict就是key value值,索引有意义,数据无序 key定义规则:a:不可变--数字.字符串.元组(可变--列表.字典)b:不能重复 ...
- 计算机网络分层(OSI七层、 TCP/IP四层)