Distinctive Character

来源:互联网 发布:java bigdecimal最大值 编辑:程序博客网 时间:2024/06/05 19:35




题意:给你n个长度为k的01串,求一个串,让这个串和其他各个串相同位置上相同字符的总数的最大值最小

题解:对于所有的源串,进行bfs,找到能到达的串中,距离这些源串最远的串,这个串就是匹配数目最大值最小的串



#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int n, m;int vis[2000000];char ch[30];void dfs(int k,int p){if (p == 1){printf("%d", k);return;}dfs(k / 2, p - 1);printf("%d", k % 2);}int main(){while (~scanf("%d%d", &n, &m)){int ma = 0, ans;memset(vis, INF, sizeof vis);queue<int>q;for (int i = 1; i <= n; i++){scanf("%s", ch);int k = 0;for (int j = 0; j < m; j++)k = k * 2 + ch[j] - '0';vis[k] = 0, q.push(k);ans = k;}while (!q.empty()){int pre = q.front();q.pop();if (vis[pre] > ma){ans = pre;ma = vis[pre];}for (int i = 0; i < m; i++){int nt = (pre ^ (1 << i));if (vis[nt] != INF) continue;vis[nt] = vis[pre] + 1;q.push(nt);}}dfs(ans,m);printf("\n");}return 0;}

原创粉丝点击