zoukankan      html  css  js  c++  java
  • UVaLive 2965 Jurassic Remains (状态压缩)

    题意:给定 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
    ", bitcount(ans));
            for(int i = 0; i < n; ++i)  if(ans & (1<<i)) printf("%d ", i+1);
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    NBUT 1120 Reimu's Teleport (线段树)
    NBUT 1119 Patchouli's Books (STL应用)
    NBUT 1118 Marisa's Affair (排序统计,水)
    NBUT 1117 Kotiya's Incantation(字符输入处理)
    NBUT 1115 Cirno's Trick (水)
    NBUT 1114 Alice's Puppets(排序统计,水)
    188 Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV
    187 Repeated DNA Sequences 重复的DNA序列
    179 Largest Number 把数组排成最大的数
    174 Dungeon Game 地下城游戏
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6529600.html
Copyright © 2011-2022 走看看