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

  1. #include <cstdio>
  2. #include <queue>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <map>
  9. #include <set>
  10. #include <ctime>
  11. #include <cmath>
  12. #define MAX 100000
  13.  
  14. using namespace std;
  15. struct node
  16. {
  17. int mark,n;
  18. node operator+(const node&b)const
  19. {
  20. return (node){mark|b.mark,n+b.n};
  21. }
  22. bool operator<(const node&a)const
  23. {
  24. return n<a.n;
  25. }
  26. };
  27. int a[],c[],n,ch,num;
  28. char s[];
  29. node maxn;
  30. map<int,node>q;
  31. map<int,node>::iterator qsum;
  32. void dfs(int x,int mark,int sum,int nn)
  33. {
  34. if(x==n>>)
  35. {
  36. q.insert(make_pair(sum,(node){mark,nn}));
  37. if(sum==) maxn=max(maxn,(node){mark,nn});
  38. return;
  39. }
  40. sum^=a[x];
  41. nn++;
  42. mark|=<<x;
  43. dfs(x+,mark,sum,nn);
  44. sum^=a[x];
  45. nn--;
  46. mark&=~(<<x);
  47. dfs(x+,mark,sum,nn);
  48. }
  49. void find(int x,int mark,int sum,int nn)
  50. {
  51. if(x==n)
  52. {
  53. qsum=q.find(sum);
  54. if(qsum!=q.end()) maxn=max(maxn,qsum->second+(node){mark,nn});
  55. if(sum==) maxn=max(maxn,(node){mark,nn});
  56. return;
  57. }
  58. sum^=a[x];
  59. nn++;
  60. mark|=<<x;
  61. find(x+,mark,sum,nn);
  62. sum^=a[x];
  63. nn--;
  64. mark&=~(<<x);
  65. find(x+,mark,sum,nn);
  66. }
  67. int main()
  68. {
  69. freopen("/home/user/桌面/in","r",stdin);
  70. while(scanf("%d%*c",&n)==)
  71. {
  72. memset(a,,sizeof(a));
  73. for(int i=;i<n;i++)
  74. {
  75. memset(c,,sizeof(c));
  76. scanf("%s",s);
  77. for(int j=;s[j];j++) c[s[j]-'A']++;
  78. for(int j=;j<;j++) if(c[j]&) a[i]|=<<j;
  79. }
  80. if(n==)
  81. {
  82. if(a[]) puts("0\n");
  83. else puts("1\n1");
  84. continue;
  85. }
  86. q.clear();
  87. maxn=(node){,};
  88. dfs(,,,);
  89. find(n>>,,,);
  90. if(maxn.n)
  91. {
  92. printf("%d\n",maxn.n);
  93. for(int i=,j=;i<n;i++)
  94. {
  95. if(maxn.mark&(<<i))
  96. {
  97. if(j) putchar(' ');
  98. printf("%d",i+);
  99. j=;
  100. }
  101. }
  102. printf("\n");
  103. }
  104. else puts("0\n");
  105. }
  106. //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
  107. return ;
  108. }

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. @Transactional注解详解

    默认遇到throw new RuntimeException("...");会回滚   需要捕获的throw new Exception("...");不会回滚 ...

  2. CF 602C The Two Routes(dij+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  3. 用Bash脚本将Linux普通用户添加为系统管理员

    将Linux普通用户添加为系统管理员在Gnome或KDE这样强大与完善的桌面环境下是非常简单的事情,一般来说在用户设置的对话框里就直接有相应选项.不过,出于简洁与高效的风格,自己目前并未使用这些高端但 ...

  4. mnesia

    1.模式创建 mnesia:create_schema([node()|nodes()]).集群的节点之间创建模式 2.启动和停止 application:start(mnesia). applica ...

  5. 将json文件转换为字符串

    //从给定位置读取Json文件    public   String readJson(String path){        //从给定位置获取文件        File file = new ...

  6. Asynchronous JS: Callbacks, Listeners, Control Flow Libs and Promises

    非常好的文章,讲javascript 的异步编程的. ------------------------------------------------------------------------- ...

  7. dom入门

    当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). //显示,改变html内容 document.getElementById("p1" ...

  8. URL scheme添加以及查找方式

    2.1.1  添加URL Types URL Scheme是通过系统找到并跳转对应app的一类设置,通过向项目中的info.plist文件中加入URL types可使用第三方平台所注册的appkey信 ...

  9. IIS:错误: 无法提交配置更改,因为文件已在磁盘上更改

    文件名: \\?\C:\Windows\system32\inetsrv\config\applicationHost.config 错误: 无法提交配置更改,因为文件已在磁盘上更改 通过 Micro ...

  10. Sikuli:创新的图形化编程技术

    Sikuli是一种使用截图进行UI自动化测试的技术.Sikuli包括sikul脚本,基于Jython的API以及sikuli IDE.Sikuli可以实现任何你可以在显示器上看到ui对象的自动化,你可 ...