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的更多相关文章

  1. 一道经典的面试题:如何从N个数中选出最大(小)的n个数

    转载:https://zhidao.baidu.com/question/1893908497885440140.html 这个问题我前前后后考虑了有快一年了,也和不少人讨论过.据我得到的消息,Goo ...

  2. JAVA 递归实现从n个数中选取m个数的所有组合

    这周Java课程有个小作业:Java递归实现从n个数中选取m个数的所有组合 代码如下: //其中 n 取 1,2,3,4,5 五个数, m 取 3 package javaText; public c ...

  3. C++从多n个数中选取m个数的组合

    //start 是从哪个开始取, picked代表已经取了多少个数 //process和data是全局变量数组 //语言说明比较难,我举个例子吧 //从[ 1, 2, 3, 4 ]中选取 2 个数 / ...

  4. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  5. 找出n个数中重复最多的10个数

    题目很清晰,直接上python代码 import pandas as pd import copy class BenchMark: def __init__(self): self.MIN = 10 ...

  6. hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值

    pog loves szh II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. 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 ...

  8. 从1到n整数中1的个数

    [问题]求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...

  9. LA2965侏罗纪(异或和为0的最大数字个数)

    题意:       给你n个字符串,让你在里面找到一个字符串集合使得这些字符串中所有的字母出现的次数和为偶数,输出集合的最大个数,和ASCII最小的解. 思路:       考虑到每个字符串中所有的字 ...

随机推荐

  1. tomcat 配置SSL

    本教程使用 JDK 6 和 Tomcat 7,其他版本类似. 基本步骤: 使用 java 创建一个 keystore 文件 配置 Tomcat 以使用该 keystore 文件 测试 配置应用以便使用 ...

  2. hadoop---wordcount命令

    [zznu@master file]$ hadoop jar ~/hadoop-2.5.2/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.5.2 ...

  3. Gentoo:Xorg:Failed to load module "……" 问题

    错误描述: 安装完xorg-server后,startx启动桌面环境,出现缺少模块错误. Xorg:Failed to load module "--" 查看log: cat /v ...

  4. kissy小记

    <script> KISSY.add('demo',function(S ,require, exports, module){ var Node = require('node'); v ...

  5. ThinkPHP模板

    [MVC模式] M:Model 数据模型层,负责数据操作 V:View 视图层,负责显示视图 C:Controller 控制器,实现业务逻辑 tp框架url地址可以由以下四种 http://网址/in ...

  6. User already has more than 'max_user_connections' active connections

    vi /etc/my.cnf修改  max_user_connections 参数的数值,重启 MySQL 服务器

  7. mvc的IIS 配置问题 runAllManagedModulesForAllRequests 与 HtmlFileHandler

    runAllManagedModulesForAllRequests 一般设置为false,当为true时所有的资源将进入mvc处理,无疑会给服务器加大压力. 解决办法是时使用HtmlFileHand ...

  8. Oracle Day2 过滤、排序、单行函数

    1.过滤和排序 SQL> --查询10号部门的所有员工信息 SQL> select * from emp ; 未选定行 SQL> ed SP2: 无法创建保存文件 "afi ...

  9. java 邮件

      使用java语言实现邮件的简单的发送和接受. 说明:使用Java应用程序发送E-mail比较简单,在使用下列程序之前,你需要将mail.jar和activation.jar 添加到你的CLASSP ...

  10. SCU 1069 POJ 2955 Brackets

    区间DP #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...