zoukankan      html  css  js  c++  java
  • ICPC 2017 Japan Domestic --Making Lunch Boxes(位运算枚举)

    题目描述

    Taro has been hooked on making lunch boxes recently. Taro has obtained a new lunch box recipe book today, and wants to try as many of the recipes listed in the book as possible. 
    Enough of the ingredients for all the recipes are at hand, but they all are in vacuum packs of two. If only one of them is used leaving the other, the leftover will be rotten easily, but making two of the same recipe is not interesting. Thus he decided to make a set of lunch boxes, each with different recipe, that wouldn't leave any unused ingredients. 
    Note that the book may include recipes for different lunch boxes made of the same set of ingredients. 
    How many lunch box recipes can Taro try today at most following his dogma? 
     

    输入

    The input consists of at most 50 datasets, each in the following format. 
    n m
    b1,1...b1,m 
     ... 
    bn,1...bn,m 
    The first line contains n, which is the number of recipes listed in the book, and m, which is the number of ingredients. Both n and m are positive integers and satisfy 1 ≤ n ≤ 500, 1 ≤ m ≤ 500 and 1 ≤ n × m ≤ 500. The following n lines contain the information for each recipe with the string of length m consisting of 0 or 1. bi,j implies whether the i-th recipe needs the j-th ingredient. 1 means the ingredient is needed for the recipe and 0 means not. Each line contains at least one 1. 
    The end of the input is indicated by a line containing two zeros. 
     

    输出

    For each dataset, output the maximum number of recipes Taro can try. 

    样例输入

    4 3
    110
    101
    011
    110
    7 1
    1
    1
    1
    1
    1
    1
    1
    4 5
    10000
    01000
    00100
    00010
    6 6
    111111
    011000
    100000
    000010
    100001
    100100
    0 0
    

    样例输出

    3
    6
    0
    6


    n×m小于500,n较小时枚举n,m较小时枚举m,界限为根号500=22.3=22;
    #include "bits/stdc++.h"
    
    using namespace std;
    
    const int inf = 0x3f3f3f3f;
    
    int mp[600][600];
    int dp[2][1 << 22];
    int bit[560];
    int n, m;
    
    void slove_n() {
        int ans = 0;
        int to = 1 << n;
        for (int i = 0; i < to; i++) {
            int cnt = 0;
            for (int j = 0; j <= m; j++) bit[j] = 0;
            for (int j = 0; j < n; j++) {
                if (i & (1 << j)) {
                    for (int k = 0; k < m; k++)
                        bit[k] ^= mp[j][k];
                    cnt++;
                }
            }
            int flag = 0;
            for (int j = 0; j < m; j++) flag |= bit[j];
            if (!flag) ans = max(ans, cnt);
        }
        cout << ans << endl;
    }
    
    void slove_m() {
        int to = 1 << m;
        for (int i = 0; i < n; i++) {
            bit[i] = 0;
            for (int j = 0; j < m; j++) {
                bit[i] |= (mp[i][j] << j);
            }
        }
        for (int i = 0; i < to; i++) {
            dp[0][i] = -inf;
            dp[1][i] = -inf;
        }
        dp[0][0] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < to; j++) {
                dp[(i + 1) % 2][j ^ bit[i]] = max(dp[(i + 1) % 2][j ^ bit[i]], dp[i % 2][j] + 1);
                dp[(i + 1) % 2][j] = max(dp[(i + 1) % 2][j], dp[i % 2][j]);
            }
        }
        cout << dp[n % 2][0] << endl;
    }
    
    int main() {
        freopen("input.txt", "r", stdin);
        char ch;
        while (cin >> n >> m) {
            if (n == 0 && m == 0) break;
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    cin >> ch;
                    mp[i][j] = ch - '0';
                }
            }
            if (n <= 22)
                slove_n();
            else
                slove_m();
        }
        return 0;
    }
  • 相关阅读:
    C 和 C++ 的标准库分别有自己的 locale 操作方法,C 标准库的 locale 设定函数是 setlocale(),而 C++ 标准库有 locale 类和流对象的 imbue() 方法(gcc使用zh_CN.GBK,或者zh_CN.UTF-8,VC++使用Chinese_People's Republic of China.936或者65001.)
    QCache 缓存(模板类,类似于map,逻辑意义上的缓存,方便管理,和CPU缓存无关。自动获得被插入对象的所有权,超过一定数量就会抛弃某些值)
    QBuffer简单操作(被看做一个标准的可随机访问的文件,支持信号)
    Qt里的原子操作QAtomicInteger
    进程、线程、协程、例程、过程
    net Core 2.2
    如何看源码
    code review规则
    NET Core中使用Dapper操作Oracle存储过程
    实现一个Promise
  • 原文地址:https://www.cnblogs.com/albert-biu/p/9846052.html
Copyright © 2011-2022 走看看