1. 程式人生 > >NCPC2017 Distinctive Character bfs+思維

NCPC2017 Distinctive Character bfs+思維

7632: Distinctive Character

時間限制: 1 Sec  記憶體限制: 128 MB  Special Judge
提交: 69  解決: 23
[提交] [狀態] [討論版] [命題人:admin]

題目描述

Tira would like to join a multiplayer game with n other players. Each player has a character with some features. There are a total of k features, and each character has some subset of them. 
The similarity between two characters A and B is calculated as follows: for each feature f, if both A and B have feature f or if none of them have feature f, the similarity increases by one.
Tira does not have a character yet. She would like to create a new, very original character so that the maximum similarity between Tira’s character and any other character is as low as possible.
Given the characters of the other players, your task is to create a character for Tira that fulfils the above requirement. If there are many possible characters, you can choose any of them.

 

輸入

The first line of input contains two integers n and k, where 1 ≤ n ≤ 105 is the number of players (excluding Tira) and 1 ≤ k ≤ 20 is the number of features. 
Then follow n lines describing the existing characters. Each of these n lines contains a string of k digits which are either 0 or 1. A 1 in position j means the character has the j’th feature, and a 0 means that it does not have the j’th feature.

 

輸出

Output a single line describing the features of Tira’s character in the same format as in the input.
If there are multiple possible characters with the same smallest maximum similarity, any one of them will be accepted. 

 

樣例輸入

3 5
01001
11100
10111

 

樣例輸出

00010

題意:給n個長度為m的字串,只有0和1,求一個字串使和其他字串在相同位置字母相同的總數的最大值最小

思路:記錄每個字串,推進佇列中,dis陣列記錄原字串改變了幾位,第一次訪問的一定就是最小的那個,然後在這些裡面找

到最大的

程式碼:

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e5 + 10;
int dis[1 << 21];
queue<int> que;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    string s;
    memset(dis, -1, sizeof(dis));
    for (int i = 0; i < n; i++) {
        cin >> s;
        int v = 0;
        for (int j = 0; j < m; j++) {
            if (s[j] == '1') {
                v |= (1 << j);
            }
        }
        que.push(v);
        dis[v] = 0;
    }
    int ans = 0, maxx = 0;
    while (!que.empty()) {
        int val = que.front();
        que.pop();
        for (int i = 0; i < m; i++) {
            int neww = val ^(1 << i);
            if (dis[neww] != -1) continue;
            que.push(neww);
            dis[neww] = dis[val] + 1;
            if (dis[neww] > maxx) {
                maxx = dis[neww];
                ans = neww;
            }
        }
    }
    for (int i = 0; i < m; i++) {
        if (ans & (1 << i)) cout << "1";
        else cout << "0";
    }
    puts("");
    return 0;
}