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最小的解. 思路: 考虑到每个字符串中所有的字 ...
随机推荐
- MVC中AuthConfig的作用 -- ASP.NET MVC 4 使用 OAuth
ASP.NET MVC 4 使用 OAuth 这个教程向你展示了如何创建一个ASP.NET MVC 4的web应用,能让用户用外部提供方的证书(比如Facebook, Twitter, Microso ...
- JS预览图像将本地图片显示到浏览器上
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 2014web面试题
面试题目会根据你的等级和职位变化,入门级到专家级:范围↑.深度↑.方向↑; 类型: 技术视野.项目细节.理论知识型题,算法题,开放性题,案例题. 进行追问,可以确保问到你开始不懂或者面试官开始不懂为止 ...
- Java "==" 和 "equals" 和 "" 问题
//equals()方法出现的问题 String a="testd"; String b="testd"; String c=new String(" ...
- NOIP2010-普及组复赛-第四题-三国游戏
题目描述 Description 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不 ...
- 【笔记】虚拟机用Xshell登陆报错“ssh服务器拒绝了密码”解决方法
自己本地安装了VMware Workstation之后,创建虚拟机,一开始是可以用Xshell登陆的,几天后,重启了电脑以及虚拟机,发现无法登陆了,还没有输入密码就有如下报错信息 这时候开始排查原因, ...
- display flex 和a标签不行
父元素display: flex; display: -webkit-flex; flex-flow: row wrap; -webkit-flex-flow: row wrap; 配合子元素 fl ...
- Python 学习笔记5
Life is like a box of chocolate. 今天继续学习Python数据结构. http://www.pythondoc.com/pythontutorial3/datastru ...
- HTTP SOAP Request
public string SoapRequest(string url, string message, string type, Encoding encoding) { string resul ...
- 第19讲 不带参数功能FC的编程与应用