【搜索+思维】Distinctive Character

题目描述

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

思路:

  首先对于一个串(假设10110),对于它能走到的串有5个分别是,00110,11110,10010,10100,10111,也就是只有一位与原串不一样的串。然后我们对于所有的源串,我们bfs,找到(1<<20)个串中,距离这些源串最远的串,就是匹配数目最大值最小的串。

代码如下:

 1 #include <iostream>
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 const int maxn = (1<<20) ;
 5 queue<int> q;
 6 int d[maxn];
 7 string str;
 8 inline int read()                                //读入优化
 9 {
10     int x=0,f=1;
11     char c=getchar();
12     while (!isdigit(c))
13         f=c=='-'?-1:1,c=getchar();
14     while (isdigit(c))
15         x=(x<<1)+(x<<3)+(c^48),c=getchar();
16     return x*f;
17 }
18 int n,k,maxx,als;
19 void solve(){
20     while(!q.empty())
21     {
22         int now=q.front();
23         q.pop();
24         for(int i=0;i<k;i++)
25         {
26             int nxt=now^(1<<i);
27             if(d[nxt]!=-1)
28                 continue;
29             q.push(nxt);
30             d[nxt]=d[now]+1;
31             if(d[nxt]>maxx)
32             {
33                 maxx=d[nxt];
34                 als=nxt;
35             }
36         }
37     }
38 }
39 void output()
40 {
41     int a[20];
42     for(int i=0;i<k;i++)
43     {
44         a[i]=als%2;
45         als/=2;
46     }
47     for(int i=0;i<k;i++)
48     {
49         printf("%d",a[i]);
50     }
51     cout << endl;
52 }
53 int main()
54 {
55     n=read(),k=read();
56     memset(d,-1,sizeof(d));
57     for(int i=1;i<=n;i++)
58     {
59         cin>>str;
60         int num=0;
61         for(int i=0;i<k;i++)
62         {
63             if(str[i]=='1'){
64                 num|=(1<<i);
65             }
66         }
67         q.push(num);
68         d[num]=0;
69     }
70     maxx=-1;
71     solve();
72     output();
73     //cout << "Hello world!" << endl;
74     return 0;
75 }
View Code

 

 
posted @ 2018-09-06 22:30  听风不成泣  阅读(459)  评论(0编辑  收藏  举报