您的位置:首页 > 大数据 > 人工智能

UVaLive 2965 Jurassic Remains (状态压缩)

2017-03-10 11:07 281 查看
题意:给定 n 个大写字母组成的字符串,选择尽量多的串,使得大写字母都能出现偶数次。

析:由于n比较小,我们可以枚举前n/2的所有组合,然后再从后面查找。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1LL << 60;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 24 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
map<int, int> mp;
int a[maxn];

inline int bitcount(int x){ return x ? bitcount(x>>1) + (x&1) : 0; }

int main(){
while(scanf("%d", &n) == 1){
string s;
for(int i = 0; i < n; ++i){
cin >> s;
a[i] = 0;
for(int j = 0; j < s.size(); ++j)  a[i] ^= 1 << s[j] - 'A';
}
mp.clear();
int n1 = n / 2, n2 = n - n1;
for(int i = 0; i < (1<<n1); ++i){
int tmp = 0;
for(int j = 0; j < n1; ++j)  if(i & (1<<j)) tmp ^= a[j];
if(!mp.count(tmp) || bitcount(mp[tmp]) < bitcount(i))  mp[tmp] = i;
}

int ans = 0;
for(int i = 0; i < (1<<n2); ++i){
int tmp = 0;
for(int j = 0; j < n2; ++j)  if(i & (1<<j)) tmp ^= a[n1+j];
if(mp.count(tmp) && bitcount(mp[tmp]) + bitcount(i) > bitcount(ans))  ans = mp[tmp] ^ (i<<n1);
}
printf("%d\n", bitcount(ans));
for(int i = 0; i < n; ++i)  if(ans & (1<<i)) printf("%d ", i+1);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: