LA 2965 (map的正确使用方法+MEET IN THE MIDDLE)
2017-11-20 15:59
525 查看
Problem
给定N个大写字母组成的字符串,要求选择尽量多的串,使得每个大写字母出现偶数次Data constraint
n <= 40Solution
很显然中途相遇法我们枚举前n/2个的状态,存在一个MAP里,然后再后n/2个直接查找
注意map的正确使用方法,我的理解是相当于一个hash,只不过代码短了. 当然还有几种用法,慢慢积累
map<int,int> table; //这就定义了一个map
table.count(x)表示x这个数字是否出现过 table[x]=y表示x这个位置赋值为y int bitcount (int x){ return x==0 ? 0 : bitcount(x/2) + (x&1) ;} 再看看这个快速算1的个数的简便打法!!
#include <cstdio> #include <cstring> #include <algorithm> #include <map> using namespace std; #define fo(i,a,b) for(i=a;i<=b;i++) const int Maxn = 24; map<int,int>table; int n,i,j,a[Maxn]; char s[1000]; int bitcount (int x){ return x==0 ? 0 : bitcount(x/2) + (x&1) ;} int main() { scanf("%d",&n); fo(i,0,n){ scanf("%s",s+1), a[i]=0; for (j=1;s[j]!='\0';j++) a[i] ^= (1<<(s[j]-'A')); } table.clear(); int n1=n/2, n2=n-n1; fo(i,0,(1<<n1)-1){ int x=0; fo(j,0,n1-1) if (i & (1 << j)) x ^= a[j]; if (!table.count(x) || bitcount(table[x]) < bitcount(i)) table[x] = i; } int ans=0; fo(i,0,(1<<n2)-1){ int x=0; fo(j,0,n2-1) if (i & (1 << j)) x ^= a[n1+j]; if (table.count(x) && bitcount(ans)<bitcount(table[x])+bitcount(i)) ans = (i << n1) + table[x]; } printf("%d\n",bitcount(ans)); fo(i,0,n) if (ans & (1<<i)) printf("%d ",i+1); }
相关文章推荐
- POJ2388《Who's in the Middle》方法:排序
- [html] PHP使用Google map web services来计算两点间的距离 -- Compute the distance between two place via Google map services in PHP
- Android ListView在增加HeaderView之后使用getLocationInWindow和getLocationOnScreen获得值不正确的解决方法
- 在使用apt-get安装报错:To continue type in the phrase 'Yes, do as I say!' ?] 解决方法
- NOI2001方程的解数 (DFS技巧:meet in the middle)(非哈希)
- UVa 1326 Jurassic Remains 解题报告(Meet-in-the-Middle)
- Linux使用yum安装时出现The program package-cleanup is found in the yum-utils package.解决方法
- 【A*/Meet in the middle/BFS模板】8数码问题
- map is not in the sudoers file.This incident will be reported.的解决方法
- codeforces 525E Anya and Cubes(暴力,meet-in-the-middle)
- 【Topcoder SRM523】【meet in the middle】 AlphabetPaths 搜索
- 《精通SOA》连载:7.7.1 在RAD中创建meet-In-the-Middle的Mapping关系
- 【TC-SRM461Div1】Fencing Garden【Meet In The Middle】【二分】
- [USACO 2012OPEN] Balanced Cow Subsets(meet in the middle + DFS)
- SRM 572 D1L2:EllysBulls,Brute Force,meet in the middle
- 【meet in the middle深度优先搜索】 NOI2001方程的解数
- 使用Struts2的标签报“警告: No configuration found for the specified action: 'login.action' in ...”的处理方法
- Meet-in-the-middle思想的一些应用
- map(映射)的使用方法 (The usage of map.)
- poj 1903 meet in the middle部分枚举思想