Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
typedef bitset<128> Bit;
vector<Bit> dat;
unordered_map<Bit,int> memo;
int M, N;
int dfs(const Bit& rest){
if(memo.count(rest)) return memo[rest];
int& res = memo[rest];
res = 1e9;
if(rest.none() || rest.count() == 1) return res = 0;
REP(k,M){
Bit y = rest, n = rest;
REP(i,N){
if(rest.test(i)){
if(dat[i].test(k))
n.reset(i);
else
y.reset(i);
}
}
if(y == rest || n == rest) continue;
res = min(res, max(dfs(y)+1, dfs(n)+1));
}
return res;
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
while(cin>>M>>N,M){
memo.clear();
dat.clear();
REP(i,N){
string s; cin >> s;
dat.emplace_back(s);
}
int ans = dfs(Bit(string(N, '1')));
/*
for(int bit=0;bit<1<<M;++bit){
bool ok = true;
set<string> memo;
for(int i=0;i<N;++i){
for(int j=i+1;j<N;++j){
int k;
for(k=0;k<M;++k)
if((bit>>k&1) && fet[i][k] != fet[j][k])
break;
if(k == M){
ok = false;
i = j = N;
}
}
if(ok) ans = min(ans, bc(bit));
}
*/
cout << ans << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <numeric>
#define IINF 1000000000
#define INF 9223372036854775807
#define MOD 998244353
#define mod 998244353
#define INT_MAX_ 2147483647
#define EPS (1e-10)
#define REP(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
#define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define me memset
#define bit(n,k) ((n>>k)&1)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
int dx[4]={-1,0,1,0};
int dy[4]={0,-1,0,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp1(pair<ll,ll> a,pair<ll,ll> b){
if(a.se!=b.se)
return a.se<b.se;
else
return a.fi>b.fi;
}
//----------------------------------------------------------------------
int M, N;
int character[150][15];
map<V<int>, int> m;
int dfs(V<int> S) {
if (S.size() <= 1) return 0;
int ret = m[S];
if (ret > 0) return ret;
ret = M;
for (int i = 0; i < M; i++) {
V<int> left, right;
for (int v : S) {
if (character[v][i]) left.push_back(v);
else right.push_back(v);
}
if (left.empty() || right.empty()) continue;
ret = min(ret, 1+max(dfs(left), dfs(right)));
}
m[S]=ret;
return ret;
}
//----------------------------------------------------------------------
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
//-------------------------------
//ll begin_t=clock();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//-------------------------------
while (cin >> M >> N) {
if (M == 0 && N == 0) break;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
for (int j = 0; j < M; j++) {
character[i][j] = s[j] - '0';
}
}
m.clear();
vector<int> S(N);
for (int i = 0; i < N; i++)
S[i] = i;
cout << dfs(S) << endl;
}
//-------------------------------
//fclose(stdin);
//fclose(stdout);
//ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
//-------------------------------
return 0;
}
//----------------------------------------------------------------------
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <numeric>
#include <cstdlib>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <memory>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <memory.h>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <memory>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
const double EPS = 1E-9;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef complex<double> xy_t;
const int INF = 1 << 30;
string nums[1000];
int memo[1 << 22];
int n, m;
int calc(int state){
if(memo[state] != -1) return memo[state];
else{
int rest = 0;
rep(i, n){
bool ok = true;
rep(j, m){
int s = (state >> (j * 2)) & 3;
if(s == 2 && nums[i][j] == '0') ok = false;
if(s == 1 && nums[i][j] == '1') ok = false;
}
if(ok) rest++;
}
if(rest == 0){
return memo[state] = INF;
}else if(rest == 1){
return memo[state] = 0;
}
}
int res = INF;
rep(i, m){
int s = (state >> (i * 2)) & 3;
if(s == 0){
int a = calc(state | (1 << (i * 2)));
int b = calc(state | (2 << (i * 2)));
if(a == INF && b != INF) res = min(res, b + 1);
else if(b == INF && a != INF) res = min(res, a + 1);
else if(a != INF && b != INF) res = min(res, max(a, b) + 1);
}
}
return memo[state] = res;
}
int main(){
while(cin >> m >> n && (m || n)){
memset(memo, -1, sizeof(memo));
rep(i, n){
cin >> nums[i];
}
cout << calc(0) << endl;;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#pragma comment (linker, "/STACK:526000000")
#include "bits/stdc++.h"
using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()
void init() {
iostream::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
unsigned long xor128() {
static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
unsigned long t = (x ^ (x << 11));
x = y; y = z; z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
//li chao tree by ei1333. It's public available.
// https://ei1333.github.io/luzhiled/snippets/structure/li-chao-tree.html
/*
template< typename T >
struct LiChaoTree {
struct Line {
T a, b;
Line(T a, T b) : a(a), b(b) {}
inline T get(T x) const { return a * x + b; }
inline bool over(const Line& b, const T& x) const {
return get(x) < b.get(x);
}
};
vector< T > xs;
vector< Line > seg;
int sz;
LiChaoTree(const vector< T >& x, T INF) : xs(x) {
sz = 1;
while (sz < xs.size()) sz <<= 1;
while (xs.size() < sz) xs.push_back(xs.back() + 1);
seg.assign(2 * sz - 1, Line(0, INF));
}
void update(Line& x, int k, int l, int r) {
int mid = (l + r) >> 1;
auto latte = x.over(seg[k], xs[l]), malta = x.over(seg[k], xs[mid]);
if (malta) swap(seg[k], x);
if (l + 1 >= r) return;
else if (latte != malta) update(x, 2 * k + 1, l, mid);
else update(x, 2 * k + 2, mid, r);
}
void update(T a, T b) { // ax+b
Line l(a, b);
update(l, 0, 0, sz);
}
T query(int k) { // xs[k]
const T x = xs[k];
k += sz - 1;
T ret = seg[k].get(x);
while (k > 0) {
k = (k - 1) >> 1;
ret = min(ret, seg[k].get(x));
}
return ret;
}
};
#define int long long
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> landmarks;
vector<vector<int>> costs;
REP(i, n) {
vector<int> tmp;
REP(q, m) {
int b;
cin >> b;
b = xor128() % 10000000;
tmp.push_back(b);
}
landmarks.push_back(tmp);
}
REP(i, n) {
vector<int> tmp;
{
REP(q, m) {
int b;
cin >> b;
b = xor128() % 100000000;
tmp.push_back(b);
}
}
costs.push_back(tmp);
}
vector<LiChaoTree<int>> yoko; // size should be m
REP(i, n) {
vector<int> hoge(m);
REP(q, m) {
hoge[q] = q;
}
LiChaoTree<int> now(hoge, 1e18);//size should be n
yoko.push_back(now);
}
vector<vector<int>> dp = landmarks;
for (int q = 0; q < m; ++q) {
vector<int> hoge(n);
REP(i, n) {
hoge[i] = i;
}
LiChaoTree<int> now(hoge, 1e18);//size should be n
for (int i = 0; i < n; ++i) {
int now_min = costs[0][0] * (i + q);
if (q < 0) {
int geko = yoko[i].query(q);
now_min = min(now_min, geko);
}
if (i != 0) {
int geko = now.query(i);
now_min = min(now_min, geko);
}
dp[i][q] = now_min;
yoko[i].update(costs[i][q], landmarks[i][q] + dp[i][q] - q * costs[i][q]);
now.update(costs[i][q], landmarks[i][q] + dp[i][q] - i * costs[i][q]);
}
}
cout << dp[n - 1][m - 1] << endl;
}
*/
#define int ll
int dp[177147];
int bit[100];
int n, m;
vector<int> inputs;
int dfs(int now) {
if (dp[now] != -1) return dp[now];
int diff = 0;
int comp = 0;
int hoge = now;
for (int q = 0; q < n; ++q) {
if (hoge % 3 != 2) {
comp += (1 << q);
diff += (1 << q) * (hoge % 3);
}
hoge /= 3;
}
int cnt = 0;
REP(i, inputs.size()) {
if ((inputs[i] & comp) == diff) {
cnt++;
}
}
if (cnt == 1) return dp[now] = 0;
else if(cnt == 0) return dp[now] = -1e9;
int ans = 100;
hoge = now;
for (int q = 0; q < n; ++q) {
if (hoge % 3 == 2) {
int tmp = now - bit[q] * 2;
int tea = max(dfs(tmp), dfs(tmp + bit[q]));
if (tea == -1e9) continue;
ans = min(ans,tea + 1);
}
hoge /= 3;
}
return dp[now] = ans;
}
void solve(){
bit[0] = 1;
for (int q = 1; q < 13; ++q) {
bit[q] = bit[q - 1] * 3;
}
while (true) {
cin >> n >> m;
if (n == 0) return;
REP(i, 177147) {
dp[i] = -1;
}
inputs.clear();
REP(i, m) {
string s;
cin >> s;
int a = 0;
REP(q, s.length()) {
if (s[q] == '1') {
a += (1 << q);
}
}
inputs.push_back(a);
}
cout << dfs(bit[n] - 1) << endl;
}
}
#undef int
int main() {
init();
solve();
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
public class Main {
static IO io = new IO();
public static void main(String[] args) {
while(solve());
io.flush();
}
static int mask = 0;
static int[] memo;
public static boolean solve() {
int n = io.nextInt();
int m = io.nextInt();
if (n == 0) return false;
int[] a = new int[m];
for(int i=0;i<m;i++) {
int x = Integer.parseInt(io.next(),2);
a[i] = convert(n,x);
// System.err.println(Integer.toBinaryString(x));
// System.err.println(Integer.toBinaryString(a[i]));
}
mask = mask(n*2);
memo = new int[1<<2*n];
Arrays.fill(memo, -1);
io.println(f(mask,n,m,a));
return true;
}
public static int convert(int n,int x) {
int y = 0;
for(int i=0;i<n;i++) {
y |= 1 << (2 * i + ((x >> i) & 1));
}
return y;
}
public static int mask(int nBits) {
return (1 << nBits) - 1;
}
public static int f(int s, int n, int m, int[] a) {
if (memo[s] >= 0) {
return memo[s];
}
int count = 0;
int and = ~0;
for(int i=0;i<m;i++) {
if ((~s & a[i] & mask) == 0) {
count++;
and &= a[i];
}
}
if (count <= 1) {
return memo[s] = 0;
}
int ans = Integer.MAX_VALUE;
for(int i=0;i<n;i++) {
if ((and >> i*2 & 3) > 0) {
continue;
}
int max = Math.max(f(s & ~(1<<i*2), n, m, a),
f(s & ~(1<<i*2+1), n, m, a)) + 1;
ans = Math.min(ans,max);
}
return memo[s] = ans;
}
}
class IO extends PrintWriter {
private final InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
public IO() { this(System.in);}
public IO(InputStream source) { super(System.out); this.in = source;}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
}else{
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
private static boolean isNewLine(int c) { return c == '\n' || c == '\r';}
public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
public boolean hasNextLine() { while(hasNextByte() && isNewLine(buffer[ptr])) ptr++; return hasNextByte();}
public String next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
StringBuilder sb = new StringBuilder();
int b = readByte();
while(isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public char[] nextCharArray(int len) {
if (!hasNext()) {
throw new NoSuchElementException();
}
char[] s = new char[len];
int i = 0;
int b = readByte();
while(isPrintableChar(b)) {
if (i == len) {
throw new InputMismatchException();
}
s[i++] = (char) b;
b = readByte();
}
return s;
}
public String nextLine() {
if (!hasNextLine()) {
throw new NoSuchElementException();
}
StringBuilder sb = new StringBuilder();
int b = readByte();
while(!isNewLine(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext()) {
throw new NoSuchElementException();
}
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while(true){
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
}else if(b == -1 || !isPrintableChar(b)){
return minus ? -n : n;
}else{
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
throw new NumberFormatException();
}
return (int) nl;
}
public char nextChar() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return (char) readByte();
}
public double nextDouble() { return Double.parseDouble(next());}
public int[] nextIntArray(int n) { int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a;}
public long[] nextLongArray(int n) { long[] a = new long[n]; for(int i=0;i<n;i++) a[i] = nextLong(); return a;}
public double[] nextDoubleArray(int n) { double[] a = new double[n]; for(int i=0;i<n;i++) a[i] = nextDouble(); return a;}
public void nextIntArrays(int[]... a) { for(int i=0;i<a[0].length;i++) for(int j=0;j<a.length;j++) a[j][i] = nextInt();}
public int[][] nextIntMatrix(int n,int m) { int[][] a = new int[n][]; for(int i=0;i<n;i++) a[i] = nextIntArray(m); return a;}
public char[][] nextCharMap(int n,int m) { char[][] a = new char[n][]; for(int i=0;i<n;i++) a[i] = nextCharArray(m); return a;}
public void close() { super.close(); try {in.close();} catch (IOException e) {}}
}
| JAVA |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
const int INF=1<<29;
int m,n,obj[128];
int dp[1<<11][1<<11];
int dfs(int asked,int S){ // asked : すでに質問した特徴, S : 知りたい物体の特徴 ( 判明した bit は 0 or 1, 不明な bit は 0 )
int cnt=0; // 今わかっている情報と矛盾しない候補の個数
rep(i,n) if((obj[i]&asked)==(S&asked)) cnt++;
if(cnt==0) return -INF;
if(cnt==1) return 0;
if(dp[asked][S]!=-1) return dp[asked][S];
int res=INF;
rep(i,m) if((asked>>i&1)==0) {
res=min(res,max(dfs(asked|1<<i,S),dfs(asked|1<<i,S|1<<i))+1);
}
return dp[asked][S]=res;
}
int main(){
for(;scanf("%d%d",&m,&n),m;){
rep(i,n){
char s[128]; scanf("%s",s);
obj[i]=0;
rep(j,m) if(s[j]=='1') obj[i]|=1<<j;
}
memset(dp,-1,sizeof dp);
printf("%d\n",dfs(0,0));
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | from collections import Counter
while True:
m, n = (int(s) for s in input().split())
if not m:
break
objects = [int(input(), 2) for i in range(n)]
dp = [bytearray(1 << m) for i in range(1 << m)]
bits = [1 << i for i in range(m)]
for asked in reversed(range((1 << m) - 1)):
for masked, count in Counter(obj & asked for obj in objects).items():
if count > 1:
dp[asked][masked] = min(max(dp[asked + b][masked],
dp[asked + b][masked + b])
for b in bits if not b & asked) + 1
print(dp[0][0]) | PYTHON3 |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr = ds.begin(); itr != ds.end(); ++itr)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 2000000000000000000LL
#define EPS 1e-9
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<double> > vvd;
typedef vector<vector<P> > vvP;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef pair<int, pair<int, int> > Edge;
typedef vector<Edge> vE;
typedef priority_queue<Edge, vector<Edge>, greater<Edge> > pqlE;
int N, M;
vvi dp;
vi a;
int solve(int asked, int ans, vi nums) {
if (dp[asked][ans] != -1)return dp[asked][ans];
if (nums.size() < 2) return 0;
int ret = INF;
rep(i, M) {
if ((asked & (1 << i)) != 0)continue;
vi yes, no;
rep(j, nums.size()) {
if ((nums[j] & (1 << i)) != 0) {
yes.push_back(nums[j]);
}
else {
no.push_back(nums[j]);
}
}
ret = min(ret, max(solve(asked | (1 << i), ans | (1 << i), yes), solve(asked | (1 << i), ans, no)) + 1);
}
return dp[asked][ans] = ret;
}
int main() {
while (true) {
cin >> M >> N;
if (M == 0)break;
dp = vvi(1 << M, vi(1 << M, -1));
a = vi(N, 0);
rep(i, N) {
ll n;
cin >> n;
rep(j, M) {
if (n % 10 == 1) {
a[i] += 1LL << j;
}
n /= 10;
}
}
cout << solve(0, 0, a) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
const int INF=1e9;
const int MOD=1e9+7;
const double EPS=1e-9;
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
return os<<'('<<p.first<<','<<p.second<<')';
}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& a){
os<<'[';
rep(i,a.size()) os<<(i?" ":"")<<a[i];
return os<<']';
}
typedef bitset<128> bset;
namespace std{
bool operator<(const bset& a,const bset& b){
rep(i,a.size()) if(a[i]!=b[i]) return a[i]<b[i];
return false;
}
bool operator>(const bset& a,const bset& b){
rep(i,a.size()) if(a[i]!=b[i]) return a[i]>b[i];
return false;
}
}
int solve(const vs& ss,bset mask,map<bset,int>& memo)
{
if(memo.count(mask)) return memo[mask];
if(mask.count()==1) return memo[mask]=0;
int n=ss.size(),m=ss[0].size();
int res=INF;
rep(i,m){
bset b;
rep(j,n) if(mask[j] && ss[j][i]=='1') b[j]=1;
if(b.none() || b==mask) continue;
res=min(res,1+max(solve(ss,b,memo),solve(ss,mask^b,memo)));
}
return memo[mask]=res;
}
int main()
{
for(int m,n;cin>>m>>n && m|n;){
vs ss(n);
rep(i,n) cin>>ss[i];
map<bset,int> memo;
cout<<solve(ss,bset(string(n,'1')),memo)<<endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
int n,m;
string obj[222];
unordered_map<int,int> memo;
int rec(int state){
if(memo.count(state))return memo[state];
int cnt = 0, tmp = state;
vector<int> col(m);
rep(i,m){ col[i] = tmp%3; tmp/=3; }
rep(i,n){
bool f = true;
rep(j,m){
if(col[j] == 1 && obj[i][j] == '1')f = false;
if(col[j] == 2 && obj[i][j] == '0')f = false;
if(!f)break;
}
if(f)cnt++;
}
if(cnt<=1)return memo[state] = 0;
int res = m;
rep(i,m){
if(col[i]==0){
int maxv = 0;
for(int k=1;k<=2;k++){
col[i] = k;
int nstate = 0;
for(int j=m-1;j>=0;j--)nstate = nstate*3 + col[j];
maxv = max(maxv, rec(nstate));
}
res = min(res, maxv);
col[i] = 0;
}
}
return memo[state] = res+1;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
while(cin >> m >> n, m){
rep(i,n)cin >> obj[i];
memo.clear();
cout << rec(0) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
using namespace std;
int n,m;
int t[128];
map<vector<int>,int> mp;
int rec(vector<int> v){
if(v.size()<=1)return 0;
if(mp.count(v)>0)return mp[v];
int res=1e9;
for(int i=0;i<n;i++){
vector<int> A,B;
for(int j=0;j<(int)v.size();j++){
int id=v[j];
if(t[id]>>i&1)A.push_back(id);
else B.push_back(id);
}
if(A.empty()||B.empty())continue;
res=min(res,max(rec(A),rec(B))+1);
}
return mp[v]=res;
}
int main(){
while(1){
cin>>n>>m;
if(n==0&&m==0)break;
mp.clear();
for(int i=0;i<m;i++){
t[i]=0;
string str;
cin>>str;
for(int j=0;j<n;j++){
t[i]*=2;
t[i]+=(str[j]-'0');
}
}
vector<int> v;
for(int i=0;i<m;i++)v.push_back(i);
cout<<rec(v)<<endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | import java.util.*;
public class Main {
private static Scanner scanner;
public static int[] inputIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; ++i) {
res[i] = scanner.nextInt();
}
return res;
}
// public static String[] <T> ToStringArray(T[] arr) {
// String[] res = new String[arr.length];
// for(int i = 0; i < arr.length; ++i) {
// res[i] = String.valueOf(arr[i]);
// }
// return res;
// }
private static List<Integer> asIntergerList(int[] arr) {
List<Integer> intList = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
intList.add(arr[i]);
}
return intList;
}
private static int[][] deepCopy(int[][] arr) {
int[][] res = new int[arr.length][];
for (int i = 0; i < arr.length; ++i) {
res[i] = arr[i].clone();
}
return res;
}
private static String asString(int[][] arr) {
String str = new String();
for (int i = 0; i < 2; ++i) {
str += "" + arr[i][0] + arr[i][1] + arr[i][2] + arr[i][3];
}
return str;
}
private static int dfs(int y, int x, int[][] arr) {
int[] dy = { -1, 1, 0, 0, 1, 1, -1, -1 };
int[] dx = { 0, 0, 1, -1, 1, -1, 1, -1 };
if (arr[y][x] == 0) {
return 0;
}
arr[y][x] = 0;
for (int i = 0; i < 8; ++i) {
int py = y + dy[i], px = x + dx[i];
if (py < 0 || py >= arr.length || px < 0 || px >= arr[0].length) {
continue;
}
if (arr[py][px] == 1) {
dfs(py, px, arr);
}
}
return 1;
}
public static class Pair<A, B> {
public A left;
public B right;
public Pair(A a, B b) {
this.left = a;
this.right = b;
}
}
public static int m, n;
public static int[] arr;
public static int[] dp;
public static int[] asArray(int stat) {
int[] stata = new int[m];
for (int i = 0; i < m; ++i, stat /= 3) {
stata[i] = stat % 3;
}
return stata;
}
public static int asInt(int[] stata) {
int ans = 0;
int t = 1;
for (int i = 0; i < stata.length; ++i, t *= 3) {
ans += t * stata[i];
}
return ans;
}
public static ArrayList<Integer> getGroup(int[] stata) {
ArrayList<Integer> candidates = new ArrayList<Integer>();
nextCand: for (int i = 0; i < n; ++i) {
for (int j = 0; j < stata.length; ++j) {
if (stata[j] != 2) {
if (((arr[i] >> j) & 1) != stata[j]) {
continue nextCand;
}
}
}
candidates.add(arr[i]);
}
return candidates;
}
// Stat
public static int func(int stat) {
if (dp[stat] != -1) {
return dp[stat];
}
int[] stata = asArray(stat);
ArrayList<Integer> currentGroup = getGroup(stata);
if (currentGroup.size() <= 1) {
return 0;
}
int ans = 1000000;
for (int i = 0; i < m; ++i) {
if (stata[i] == 2) {
stata[i] = 0;
int ans0 = func(asInt(stata));
stata[i] = 1;
int ans1 = func(asInt(stata));
stata[i] = 2;
int tmp = Math.max(ans0, ans1) + 1;
if (tmp < ans) {
ans = tmp;
}
}
}
dp[stat] = ans;
return ans;
}
public static void main(String[] args) {
scanner = new Scanner(System.in);
while (true) {
m = scanner.nextInt();
n = scanner.nextInt();
if (m == 0 && n == 0) {
return;
}
arr = new int[n];
for (int i = 0; i < n; ++i) {
String s = scanner.next();
// System.out.println(s);
int v = 0;
for (int j = 0; j < m; ++j) {
if (s.charAt(j) == '1') {
v |= (1 << j);
}
}
arr[i] = v;
}
dp = new int[200000];
for (int i = 0; i < dp.length; ++i) {
dp[i] = -1;
}
int[] stata = new int[m];
for (int i = 0; i < m; ++i) {
stata[i] = 2;
}
System.out.println(func(asInt(stata)));
}
}
}
| JAVA |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
for(int i=0;i<(int)(s.size());++i) {
cout << s[i];
if(i == (int)(s.size())-1) cout << endl;
else cout << " ";
}
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
os << p.first << ":" << p.second;
return os;
}
map<vi, int> mp;
int sch(vi v, const vvi &ch, int n, int m) {
if((int)(v.size()) <= 1) return 0;
int ret = mp[v];
if(ret > 0) return ret;
ret = m;
for(int i=0;i<m;++i) {
vi left, right;
for(auto &e: v) {
if(ch[e][i]) left.push_back(e);
else right.push_back(e);
}
if(!left.empty() && !right.empty()) ret = min(ret, max(sch(left, ch, n, m), sch(right, ch, n, m)) + 1);
}
return mp[v] = ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
while(1) {
int n, m; cin >> m >> n;
if(n == 0 && m == 0) break;
vvi ch(n, vi(m));
for(int i=0;i<n;++i) {
string s; cin >> s;
for(int j=0;j<m;++j) {
ch[i][j] = s[j] - '0';
}
}
vi v(n);
for(int i=0;i<n;++i) {
v[i] = i;
}
mp.clear();
cout << sch(v, ch, n, m) << endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int m, n;
int rec(vector<int> rest, map<vector<int>, int>& memo) {
if(memo.count(rest) == 1) {
return memo[rest];
}
int& res = memo[rest];
if(rest.size() <= 1) {
return res = 0;
}
res = m;
for(int i=0; i<m; ++i) {
vector<int> rest1, rest2;
for(auto val : rest) {
if((val >> i) & 1) {
rest1.push_back(val);
} else {
rest2.push_back(val);
}
}
res = min(res, max(rec(rest1, memo), rec(rest2, memo)) + 1);
}
return res;
}
int main() {
while(cin >> m >> n, m) {
vector<int> v(n);
for(int i=0; i<n; ++i) {
string s;
cin >> s;
int t = 0;
for(int i=0; i<m; ++i) {
t <<= 1;
t |= s[i] == '1';
}
v[i] = t;
}
map<vector<int>, int> memo;
cout << rec(v, memo) << endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define FI first
#define SE second
using namespace std;
typedef long long LL;
int dp[1 << 11][1 << 11];
int num[150];
char str[15];
int n, m;
int solve(int x, int y) {
if(dp[x][y] != -1) return dp[x][y];
int cnt = 0;
for(int i = 0; i < n; ++i) cnt += (num[i] & x) == y;
if(cnt <= 1) return dp[x][y] = 0;
int &res = dp[x][y];
res = INF;
for(int i = 0; i < m; ++i) {
if(x & (1 << i)) continue;
res = min(res, 1 + max(solve(x | 1 << i, y), solve(x | 1 << i, y | 1 << i)));
}
return res;
}
int main() {
while(scanf("%d%d", &m, &n)) {
if(n + m == 0) break;
for(int i = 0; i < n; ++i) {
scanf("%s", str);
num[i] = 0;
for(int j = 0; j < m; ++j) num[i] = num[i] * 2 + str[j] - '0';
}
memset(dp, -1, sizeof(dp));
printf("%d\n", solve(0, 0));
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <numeric>
using namespace std;
int m;
vector<vector<bool>> obj;
map<vector<int>, int> memo;
int dfs(vector<int>& set) {
if (set.size() == 1) return 0;
if (memo[set]) return memo[set];
memo[set] = m;
for (int f = 0; f < m; f++) {
vector<int> ok, ng;
for (int i: set) (obj[i][f] ? ok : ng).emplace_back(i);
if (ok.size() == 0 || ng.size() == 0) continue;
memo[set] = min(memo[set], max(dfs(ok), dfs(ng)) + 1);
}
return memo[set];
}
int main() {
int n;
while (cin >> m >> n, n) {
obj.assign(n, vector<bool>(m));
for (int i = 0; i < n; i++) {
string buf; cin >> buf;
for (int f = 0; f < m; f++) obj[i][f] = buf[f] - '0';
}
memo.clear();
vector<int> set(n);
iota(set.begin(), set.end(), 0);
cout << dfs(set) << endl;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
using namespace std;
map<set<int>,int>memo;
int M,N;
vector<string>v;
int dfs(set<int> &t){
if(t.size()==1)return 0;
if(memo.find(t)!=memo.end())return memo[t];
int r=1<<30;
for(int j=0;j<M;j++){
set<int>a,b;
for(auto &e:t)(v[e][j]=='0'?a:b).insert(e);
if(a.size()&&b.size())r=min(r,max(dfs(a),dfs(b))+1);
}
return memo[t]=r;
}
int main(){
for(;cin>>M>>N,M;){
v.resize(N);
for(int i=0;i<N;i++)cin>>v[i];
set<int>t;
for(int i=0;i<N;i++)t.insert(i);
memo.clear();
printf("%d\n",dfs(t));
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
int m, n, in[128];
int dp[1600000], pw[13];
int rec(int bit){
int &res = dp[bit];
if(res >= 0) return res;
int t[13] = {}, cnt = 0;
rep(j, m) t[j] = bit / pw[j] % 3;
rep(i, n){
bool ok = 1;
rep(j, m) if(t[j] < 2 && (in[i] >> j & 1) != t[j]){
ok = 0;
break;
}
if(ok) cnt++;
}
if(cnt <= 1) return res = 0;
res = inf;
rep(i, m) if(t[i] == 2){
res = min(res, 1 + max(rec(bit - 2 * pw[i]), rec(bit - pw[i])));
}
return res;
}
int main(){
pw[0] = 1;
rep(i, 12) pw[i + 1] = pw[i] * 3;
while(cin >> m >> n, m){
rep(i, n){
char buf[20];
cin >> buf;
in[i] = 0;
rep(j, m) if(buf[j] == '1') in[i] |= 1 << j;
}
memset(dp, -1, sizeof(dp));
cout << rec(pw[m] - 1) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reg(i,a,b) for(int i=(a);i<=(b);i++)
#define irep(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define ireg(i,a,b) for(int i=(b);i>=(a);i--)
typedef long long int lli;
typedef pair<int,int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
int n,m;
char s[200][200];
int dp[200000];
int pow(int p,int q){
int res=1;
rep(i,q)res*=p;
return res;
}
int main(void){
for(;;){
scanf("%d%d",&n,&m);
if(n==0)break;
rep(i,m)scanf("%s",s[i]);
memset(dp,0,sizeof(dp));
int nl=pow(3,n);
int ans=100;
//printf("nl .. %d\n",nl);
rep(i,nl){
int nb[15]={},ii=i;
rep(j,n){
nb[j]=ii%3;
ii/=3;
}
/*
rep(j,n){
printf("%d ",nb[j]);
}
printf("\n");*/
int ns=0;
rep(j,m){
bool ok=true;
rep(k,n){
if(nb[k]!=2 && (((char)nb[k])+'0')!=s[j][k])ok=false;
}
if(ok)ns++;
}
if(ns<=1){
if(ns==1)dp[i]=0;
else dp[i]=1000;
//printf("%d\n",i);
continue;
}
int nmin=100;
int tb[15]={};
rep(j,n){
tb[j]=nb[j];
//printf("%d ",tb[j]);
}
//printf(" ..tb\n");
rep(j,n){
if(tb[j]!=2)continue;
int ne=-100;
rep(p,2){
tb[j]=p;
int t=0,bas=1;
rep(k,n){
t+=tb[k]*bas;
bas*=3;
}
//if(i==8)printf("tot .. %d\n",t);
ne=max(ne,dp[t]);
tb[j]=nb[j];
}
nmin = min(ne,nmin);
//printf("doing %d %d %d\n", i,j,ne);
}
dp[i]=nmin+1;
//printf("do .. %d %d %d \n",i,ns,dp[i]);
}
printf("%d\n",dp[pow(3,n)-1]);
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define MAX 130
using namespace std;
const int IINF = INT_MAX;
int m,n;
string G[MAX];
map<deque<int>,int> memo;
int dfs(const deque<int> &obj){
if( obj.size() <= 1 ) return 0;
if( memo.find(obj) != memo.end() ) return memo[obj];
int ret = IINF;
rep(i,m){
deque<int> one,zero;
rep(j,(int)obj.size()){
if( G[obj[j]][i] == '1' ) one.push_back(obj[j]);
else zero.push_back(obj[j]);
}
if( one.empty() || zero.empty() ) continue;
ret = min(ret,max(dfs(one),dfs(zero))+1);
}
return memo[obj] = ret;
}
int main(){
while(cin >> m >> n,n|m){
rep(i,n)cin >> G[i];
memo.clear();
deque<int> obj(n);
rep(i,n) obj[i] = i;
cout << dfs(obj) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 |
import java.io.*;
import java.util.*;
public class Main {
int M, N;
BitSet[] bits;
HashMap<BitSet, Integer> memo = new HashMap<>();
public int dfs(BitSet group){
if(group.cardinality() < 2) return 0;
if(memo.containsKey(group)){
return memo.get(group);
}
int ans = 100000;
for(int i = 0; i < M; i++){
BitSet b1 = new BitSet(N);
BitSet b2 = new BitSet(N);
b1.or(group);
b1.and(bits[i]);
b2.or(group);
b2.andNot(bits[i]);
if(b1.cardinality() == 0 || b2.cardinality() == 0) continue;
ans = Math.min(ans, Math.max(dfs(b1), dfs(b2)) + 1 );
}
memo.put(group, ans);
return ans;
}
public void solve() {
while(true){
M = nextInt();
N = nextInt();
if(M == 0) break;
bits = new BitSet[M];
memo.clear();
for(int i = 0; i < M; i++){
bits[i] = new BitSet(N);
}
for(int i = 0; i < N; i++){
String str = next();
for(int j = 0; j < M; j++){
if(str.charAt(j) == '1'){
bits[j].set(i);
}
}
}
BitSet mask = new BitSet(N);
mask.set(0, N);
out.println(dfs(mask));
}
}
private static PrintWriter out;
public static void main(String[] args) {
out = new PrintWriter(System.out);
new Main().solve();
out.flush();
}
public static int nextInt() {
int num = 0;
String str = next();
boolean minus = false;
int i = 0;
if(str.charAt(0) == '-'){
minus = true;
i++;
}
int len = str.length();
for(; i < len; i++){
char c = str.charAt(i);
if(!('0' <= c && c <= '9')) throw new RuntimeException();
num = num * 10 + (c - '0');
}
return minus ? -num : num;
}
public static long nextLong() {
long num = 0;
String str = next();
boolean minus = false;
int i = 0;
if(str.charAt(0) == '-'){
minus = true;
i++;
}
int len = str.length();
for(; i < len; i++){
char c = str.charAt(i);
if(!('0' <= c && c <= '9')) throw new RuntimeException();
num = num * 10l + (c - '0');
}
return minus ? -num : num;
}
public static String next() {
int c;
while(!isAlNum(c = read())){}
StringBuilder build = new StringBuilder();
build.append((char)c);
while(isAlNum(c = read())){
build.append((char)c);
}
return build.toString();
}
private static byte[] inputBuffer = new byte[1024];
private static int bufferLength = 0;
private static int bufferIndex = 0;
private static int read() {
if(bufferLength < 0) throw new RuntimeException();
if(bufferIndex >= bufferLength){
try{
bufferLength = System.in.read(inputBuffer);
bufferIndex = 0;
}catch(IOException e){
throw new RuntimeException(e);
}
if(bufferLength <= 0) return(bufferLength = -1);
}
return inputBuffer[bufferIndex++];
}
private static boolean isAlNum(int c) {
return '!' <= c && c <= '~';
}
}
class IntMatrix {
public final int row;
public final int colum;
int[] data;
public IntMatrix(int row, int colum){
this.row = row;
this.colum = colum;
this.data = new int[row * colum];
}
public int index(int r, int c){
if(0 <= r && r < row && 0 <= c && c <= colum){
return r * colum + c;
}else{
throw new IndexOutOfBoundsException("Matrix size row: " + row + " colum:" + colum + "access: (" + r + "," + c + ")");
}
}
public void set(int r, int c, int v){
this.data[index(r,c)] = v;
}
public int get(int r, int c){
return this.data[index(r,c)];
}
public static IntMatrix identity(int n){
IntMatrix mat = new IntMatrix(n, n);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i == j) mat.set(i, j, 1);
else mat.set(i, j, 0);
}
}
return mat;
}
public IntMatrix mul(IntMatrix mat){
if(this.colum != mat.row) throw new ArithmeticException();
IntMatrix ans = new IntMatrix(this.row, mat.colum);
for(int i = 0; i < ans.row; i++){
for(int j = 0; j < ans.colum; j++){
int sum = 0;
for(int k = 0; k < this.colum; k++){
sum += get(i, k) * mat.get(k, j);
}
ans.set(i, j, sum);
}
}
return ans;
}
public IntVector mul(IntVector vec){
if(this.colum != vec.size) throw new ArithmeticException();
IntVector ans = new IntVector(this.row);
for(int i = 0; i < ans.size; i++){
int sum = 0;
for(int j = 0; j < this.colum; j++){
sum += get(i, j) * vec.get(j);
}
ans.set(i, sum);
}
return ans;
}
public IntMatrix pow(int n){
if(this.colum != this.row) throw new ArithmeticException();
if(n == 0) return IntMatrix.identity(this.row);
if(n % 2 == 0) return (this.mul(this)).pow(n / 2);
else return this.mul(this.pow(n - 1));
}
public String toString(){
StringBuilder build = new StringBuilder();
build.append("[");
for(int i = 0; i < row; i++){
build.append("[");
for(int j = 0; j < colum; j++){
if(j != 0) build.append(",");
build.append(get(i, j));
}
build.append("]");
if(i != row - 1) build.append(",");
build.append("\n");
}
return build.toString();
}
}
class IntVector{
public final int size;
private int[] data;
public IntVector(int size){
this.size = size;
data = new int[size];
}
public void set(int i, int v){
this.data[i] = v;
}
public int get(int i){
return this.data[i];
}
} | JAVA |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
vector<string> answers;
vector<int> groups;
map<vector<int>, int> dp;
int total_questions;
int total_ppl;
int dfs(const vector<int>& current,int used){
if(current.size() <= 0) return -1;
if(current.size() == 1) return 0;
if(dp.count(current)) return dp[current];
int res = INF;
for(int question_i=0;question_i<total_questions;question_i++){
if(used & (1<<question_i)) continue;
vector<int> next[256];
for(int i = 0; i < current.size(); i++){
int ppl_i = current[i];
char ans = answers[ppl_i][question_i];
next[ans].push_back(ppl_i);
}
int cost = max(dfs(next['0'],
used | (1<<question_i)) + 1,
dfs(next['1'],
used | (1<<question_i)) + 1);
res = min(res,cost);
}
return (dp[current] = res);
}
int main(){
while(~scanf("%d %d",&total_questions,&total_ppl)){
if(total_questions == 0 && total_ppl == 0) break;
groups.clear();
answers.clear();
dp.clear();
for(int ppl_i = 0; ppl_i < total_ppl; ppl_i++){
string ans;
cin >> ans;
answers.push_back(ans);
groups.push_back(ppl_i);
}
printf("%d\n",dfs(groups,0));
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
/*
<url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1302>
問題文============================================================
=================================================================
解説=============================================================
================================================================
*/
int m,n;
map<string,int> mp;
int rec(vector<string>& obj){
if(obj.size() <= 1) return 0;
int ret = m;
string s; for(auto v:obj) s += v;
if(mp.count(s)) return mp[s];
vector<string> l,r;
for(int i = 0; i < m;i++){
l.clear(); r.clear();
for(int j = 0; j < obj.size(); j++){
if(obj[j][i] == '0') l.push_back(obj[j]);
else r.push_back(obj[j]);
}
if(l.empty() || r.empty()) continue;
ret = min(ret,1 + max(rec(l),rec(r)));
}
mp[s] = ret;
return ret;
}
int solve(){
mp.clear();
int res = 0;
vector<string> obj(n);
for(auto& in:obj) cin >> in;
res = rec(obj);
return res;
}
int main(void) {
cin.tie(0); ios_base::sync_with_stdio(false);
while(cin >> m >> n,m|n){
cout << solve() << endl;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
using namespace std;
int m, n;
string s[128];
map<vector<int>,int> memo;
int dfs(vector<int> &idx){
if( idx.size() <= 1 ) return 0;
if( memo.count(idx) ) return memo[idx];
int res = 12;
for(int i=0;i<m;i++){
vector<int> idx0, idx1;
for(int j : idx){
if(s[j][i]=='0') idx0.push_back(j);
else idx1.push_back(j);
}
if( idx0.size() == 0 || idx1.size() == 0 ) continue;
int A = dfs(idx0);
int B = dfs(idx1);
res = min( res, max( A , B ) );
}
return memo[idx] = res + 1;
}
int main(){
while(1){
cin>>m>>n;
if(!m&&!n) break;
vector<int> idx;
for(int i=0;i<n;i++){
cin>>s[i];
idx.push_back(i);
}
memo.clear();
cout<<dfs(idx)<<endl;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
using namespace std;
map<__uint128_t,int>memo;
int M,N;
vector<string>v;
int dfs(__uint128_t t){
int c=0;
for(int i=0;i<N;i++){
if(t&(__uint128_t)(1)<<i)c++;
}
if(c==1)return 0;
if(memo.find(t)!=memo.end())return memo[t];
int r=1<<30;
for(int j=0;j<M;j++){
__uint128_t a=0,b=0;
for(int i=0;i<N;i++){
__uint128_t e=(__uint128_t)(1)<<i;
if(t&e)(v[i][j]=='0'?a:b)|=e;
}
if(a&&b)r=min(r,max(dfs(a),dfs(b))+1);
}
return memo[t]=r;
}
int main(){
for(;cin>>M>>N,M;){
v.resize(N);
for(int i=0;i<N;i++)cin>>v[i];
__uint128_t t=0;
for(int i=0;i<N;i++)t|=(__uint128_t)(1)<<i;
memo.clear();
printf("%d\n",dfs(t));
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>
#define f first
#define s second
#define mp make_pair
#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)
using namespace std;
typedef unsigned int uint;
typedef long long ll;
const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};
int getInt(){
int ret = 0,c;
c = getchar();
while(!isdigit(c)) c = getchar();
while(isdigit(c)){
ret *= 10;
ret += c - '0';
c = getchar();
}
return ret;
}
int f[128];
int m,n;
char memo[1<<11][1<<11];
int solve(int mask, int np){
if(memo[mask][np] != -1) return memo[mask][np];
int cnt = 0;
REP(i,n) if((f[i] & mask) == np) cnt++;
if(cnt <= 1) return 0;
int tmp = ((1 << m) - 1) & (~mask);
int ff = tmp;
int ret = 1000;
while(ff){
int tt = ff & (-ff);
int cc = 0;
REP(i,n) if((f[i] & mask) == np)
if(f[i] & tt) cc++;
if(cc != 0 && cc != cnt){
ret = min(ret, 1 +
max(solve(mask | tt, np | tt),
solve(mask | tt, np)));
}
ff ^= tt;
}
return memo[mask][np] = ret;
}
int main(){
while(scanf("%d%d ",&m,&n), m+n){
REP(i,n){
f[i] = 0;
REP(j,m){
char c;
scanf("%c ",&c);
f[i] |= (c - '0') << j;
}
}
memset(memo,-1,sizeof(memo));
print(solve(0,0));
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef long long ll;
int main(void) {
ll M, N;
while(cin >> M >> N, N && M) {
vector<string> S(N);
REP(i, 0, N) cin >> S[i];
const ll INF = 1LL << 50;
ll m2 = M * 2;
vector<ll> dp(1LL << m2, INF);
REP(i, 0, N) {
ll s = 0;
REP(j, 0, M) {
ll x = S[i][j] - '0';
s = s | (x << (j * 2));
}
dp[s] = 0;
}
REP(s, 0, 1LL << m2) {
bool skip = false;
REP(i, 0, M) if(((s >> (i * 2)) & 3LL) == 3) skip = true;
if(skip) continue;
REP(i, 0, M) if(s & (1LL << (i * 2 + 1))) {
ll s0 = s - (1LL << (i * 2 + 1));
ll s1 = s - (1LL << (i * 2 + 1)) + (1LL << (i * 2));
ll v0 = dp[s0];
ll v1 = dp[s1];
if(v0 < INF && v1 < INF) dp[s] = min(dp[s], max(v0, v1) + 1);
else if(v0 < INF) dp[s] = min(dp[s], v0);
else if(v1 < INF) dp[s] = min(dp[s], v1);
}
}
ll ans_s = 0;
REP(i, 0, M) ans_s = ans_s | (1LL << (i * 2 + 1));
cout << dp[ans_s] << endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;
int m, n;
vector<vector<int>> feats;
map<vector<int>, int> memo;
vector<int> fix;
int dfs(){
if(memo.find(fix) != memo.end()) return memo[fix];
int num = 0;
for(int i=0;i<n;i++){
bool ok = true;
for(int j=0;j<m;j++){
if(fix[j] != 0 && fix[j] != feats[i][j]){
ok = false;
break;
}
}
if(ok) num++;
}
if(num <= 1) return 0;
int ret = m;
for(int i=0;i<m;i++){
if(fix[i] == 0){
fix[i] = 1;
int res1 = dfs();
fix[i] = 2;
int res2 = dfs();
ret = min(ret, max(res1, res2) + 1);
fix[i] = 0;
}
}
memo[fix] = ret;
return ret;
}
int main(){
while(true){
cin >> m >> n;
if(m == 0) break;
feats.assign(n, vector<int>(m, 0));
for(int i=0;i<n;i++){
string str;
cin >> str;
for(int j=0;j<str.size();j++){
if(str[j] == '1'){
feats[i][j] = 1;
}else{
feats[i][j] = 2;
}
}
}
sort(feats.begin(), feats.end());
memo.clear();
fix.assign(m, 0);
cout << dfs() << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
using namespace std;
const int MAXN=130;
const int MAXB=11;
const int MAXS=(1<<MAXB)+5;
int m, n;
struct data{
int ans;
}obj[MAXN];
short dp[MAXS][MAXS];
int dfs(int state, int now){
short &ret=dp[state][now];
if(ret!=-1) return ret;
int cnt=0;
for(int i=0; i<n; i++){
if((obj[i].ans&state)==now){
cnt++;
}
}
if(cnt<2) return ret=0;
ret=m;
for(int i=0; i<m; i++){
if(state>>i&1) continue;
ret=min((int)ret,1+max(dfs(state|(1<<i),now),dfs(state|(1<<i),now|(1<<i))));
}
return ret;
}
int main(){
while(cin>>m>>n&&(n||m)){
string str;
for(int i=0; i<n; i++){
cin>>str;
int ans=0;
for(int j=0; j<m; j++){
ans=(ans<<1)|(str[j]=='1');
}
obj[i].ans=ans;
}
memset(dp,-1,sizeof dp);
cout<<dfs(0,0)<<endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount
#define INF INT_MAX/3
#define mod 1000000007
typedef vector<int> vi;
int n,m;
vi a;
map<vi,int> mem;
int dfs(vi state){
if(state.size()<=1)return 0;
if(mem.find(state)!=mem.end())return mem[state];
int res=INF;
rep(i,m){
vi l,r;
rep(j,state.size()){
if((a[state[j]]>>i)&1)l.push_back(state[j]);
else r.push_back(state[j]);
}
if(l.size()==0||r.size()==0)continue;
minch(res,max(dfs(l),dfs(r))+1);
}
return mem[state]=res;
}
bool solve(){
cin>>m>>n;
if(m==0||n==0)return false;
a.resize(n);
vector<int> idx;
rep(i,n){
string s;
cin>>s;
a[i]=0;
rep(j,m){
if(s[j]=='1')a[i]|=1<<j;
}
idx.push_back(i);
}
mem.clear();
cout<<dfs(idx)<<endl;
return true;;
}
int main(){
while(solve());
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
using namespace std;
#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}
int en(vint &v){
int ret=0;
rep(i,v.size()){
ret=ret*3+v[i];
}
return ret;
}
int M,N;
int memo[81*81*27];
int dfs(vint lis,vint v){
int c=en(v);
if(memo[c]!=-1)return memo[c];
if(lis.size()==1)return 0;
int &ret=memo[c];
ret=1001001001;
rep(i,M){
bool ok=false;
rep(j,lis.size())if((lis[j]>>i&1)!=(lis[0]>>i&1))ok=true;
if(!ok)continue;
vint x,y;
rep(j,lis.size()){
if((lis[j]>>i&1))x.pb(lis[j]);
else y.pb(lis[j]);
}
vint a,b;
a=v;b=v;
a[i]=1;b[i]=2;
chmin(ret,max(dfs(x,a),dfs(y,b))+1);
}
return ret;
}
signed main(){
while(cin>>M>>N,M||N){
vector<int>lis;
rep(i,N){
string s;
int b;
cin>>s;
rep(j,s.size()){
b=b*2+s[j]-'0';
}
lis.pb(b);
}
vector<int>v(M);
memset(memo,-1,sizeof(memo));
cout<<dfs(lis,v)<<endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0, loop##i = (int)(n); i < loop##i; ++i)
#define SZ(x) ((int)(x.size()))
#define ALL(x) x.begin(), x.end()
#define BIN(w, x) bitset<w>(x)
int m, n;
int memo[1 << 11][1 << 11];
int bits[200];
int rec(int used, int yesno) {
auto &MEMO = memo[used][yesno];
if (MEMO != -1) return MEMO;
int cnt = 0;
REP(j, n) {
if ((bits[j] & used) == yesno) {
cnt++;
}
}
if (cnt == 0 || cnt == 1) return MEMO = 0;
int ret = 1e9;
REP(i, m) {
if (used >> i & 1) continue;
ret = min(ret,
max(rec(used | (1 << i), yesno | (1 << i)) + 1,
rec(used | (1 << i), yesno | (0 << i)) + 1)
);
}
return MEMO = ret;
}
signed main() {
while (1) {
REP(i, 1 << 11) REP(j, 1 << 11) memo[i][j] = -1;
cin >> m >> n;
if (m == 0 && n == 0) break;
REP(i, n) {
string s;
cin >> s;
bits[i] = stoll(s, nullptr, 2);
}
cout << rec(0, 0) << endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
int M, N,dp[2048][2048],POW[13];
char table[128][12];
int dfs(int question_state, int answer_state){
if(dp[question_state][answer_state] != -1) return dp[question_state][answer_state];
int count = 0,tmp;
for(int i = 0; i < N; i++){
tmp = 0;
for(int col = 0; col < M; col++){
if((question_state & (1 << col)) && table[i][col] == '1'){
tmp += POW[col];
}
}
if(tmp == answer_state)count++;
}
if(count <= 1){
return dp[question_state][answer_state] = 0;
}
int minimum = M;
int next_question_state;
for(int new_quest = 0; new_quest < M; new_quest++){
if(question_state & (1 << new_quest))continue;
next_question_state = question_state + POW[new_quest];
minimum = min(minimum,max(dfs(next_question_state,answer_state+POW[new_quest]),dfs(next_question_state,answer_state))+1);
}
return dp[question_state][answer_state] = minimum;
}
void func(){
for (int i = 0; i < N; i++) {
scanf("%s",table[i]);
}
for(int i = 0; i < POW[M];i++){
for(int k = 0; k < POW[M];k++)dp[i][k] = -1;
}
printf("%d\n",dfs(0,0));
}
int main() {
for(int i = 0; i < 13; i++)POW[i] = pow(2,i);
while(true){
scanf("%d %d",&M,&N);
if(M == 0 && N == 0)break;
func();
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main().solver();
}
int[][] memo;
int[] ans;
int n, m;
int rep(int masked, int answer) {
if (memo[masked][answer] != -1) {
return memo[masked][answer];
}
int count = 0;
for (int i = 0; i < n; i++) {
if ((ans[i] & masked) == answer) {
count++;
}
}
if (count <= 1) {
memo[masked][answer] = 0;
return 0;
}
int min = 1000;
for (int i = 0; i < m; i++) {
int shi = 1 << i;
if ((masked & shi) != 0) {
continue;
}
int z = shi;
min = Math.min(min, Math.max(rep(masked | z, answer), rep(masked | z, answer | z)));
}
memo[masked][answer] = min + 1;
return min + 1;
}
void solver() {
Scanner sc = new Scanner(System.in);
while (true) {
m = sc.nextInt();// the number of features 0<m<=11
n = sc.nextInt();// the number of objects 0<n<=128
memo = new int[1 << 11][1 << 11];
for (int i = 0; i < (1 << 11); i++) {
Arrays.fill(memo[i], -1);
}
if (m == 0 && n == 0)
break;
ans = new int[n];
for (int i = 0; i < n; i++) {
ans[i] = sc.nextInt(2);
}
int m = rep(0, 0);
System.out.println(m);
}
}
} | JAVA |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <numeric>
#include <algorithm>
#include <cstdio>
using namespace std;
map<vector<int>,int>memo;
int M,N;
vector<string>*vp;
int dfs(vector<int> &t){
if(t.size()==1)return 0;
if(memo.find(t)!=memo.end())return memo[t];
int r=1<<30;
for(int j=0;j<M;j++){
vector<int>a,b;
for(auto &e:t)(vp->at(e)[j]=='0'?a:b).push_back(e);
if(a.size()&&b.size())r=min(r,max(dfs(a),dfs(b))+1);
}
return memo[t]=r;
}
int main(){
for(;cin>>M>>N,M;){
vector<string>v(N);
vp=&v;
for(int i=0;i<N;i++)cin>>v[i];
vector<int>t(N);
iota(t.begin(),t.end(),0);
memo.clear();
printf("%d\n",dfs(t));
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;
typedef vector<int> vi;
int M, N;
int character[150][15];
map<vi, int> mp;
int dfs(vi S) {
if (S.size() <= 1) return 0;
int& ret = mp[S];
if (ret > 0) return ret;
ret = M;
for (int i = 0; i < M; i++) {
vi left, right;
for (int v : S) {
if (character[v][i]) left.push_back(v);
else right.push_back(v);
}
if (left.empty() || right.empty()) continue;
ret = min(ret, 1+max(dfs(left), dfs(right)));
}
return ret;
}
int main() {
while (cin >> M >> N) {
if (M == 0 && N == 0) break;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
for (int j = 0; j < M; j++) {
character[i][j] = s[j] - '0';
}
}
mp.clear();
vector<int> S(N);
for (int i = 0; i < N; i++)
S[i] = i;
cout << dfs(S) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <stack>
#include <bitset>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;
typedef bitset<128> Bit;
int n, m;
string input[128];
int memo[177147]; // 3^11
Bit table[11];
Bit iniS;
// qS[i] = j : iツ氾板姪堋づ個篠ソツ姪「ツづーyes/noツづ猟伉用
int dfs(int qS) {
if (memo[qS] >= 0) return memo[qS];
int tmp = qS;
int res = INF;
Bit manS = iniS;
tmp = qS;
REP(i, m) {
if (tmp%3==1)
manS &= table[i];
else if(tmp%3==2)
manS &= ~table[i];
tmp /= 3;
}
if (manS.count() <= 1) return memo[qS] = 0;
int hoge = 1;
tmp = qS;
REP(i, m) {
if (tmp%3 == 0) {
int nextqS1 = qS + hoge;
int nextqS2 = qS + hoge*2;
//cout << dfs(nextqS1) << " " << dfs(nextqS2) << endl;
res = min(res, max(dfs(nextqS1), dfs(nextqS2)) + 1);
}
hoge *= 3;
tmp /= 3;
}
// cout << manS << " ";
// tmp = qS;
// REP(i, m) {
// cout << tmp%3;
// tmp/=3;
// }
// cout << " " << res << endl;
return memo[qS] = res;
}
int main() {
while(cin>>m>>n,n||m) {
REP(i, n) cin >> input[i];
REP(j, m) {
table[j].reset();
REP(i, n)
if (input[i][j] == '1')
table[j].set(i);
}
iniS.reset();
REP(i, n) iniS.set(i);
memset(memo,-1,sizeof(memo));
cout << dfs(0) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
int n,m;
vi dp;
vs in;
vi tovi(int a){
vi out(m);
int t=0;
while(a){
out[t++]=a%3;
a/=3;
}
return out;
}
int toi(vi a){
int out=0;
int t=1;
rep(i,m){
out+=t*a[i];
t*=3;
}
return out;
}
vi q;
int f(int a){
if(dp[a]!=inf)return dp[a];
vi t=tovi(a);
int co=0;
rep(i,n){
bool h=true;
rep(j,m)if(t[j]!=2&&in[i][j]-'0'!=t[j])h=false;
co+=h;
}
if(co<=1)return dp[a]=0;
int mi=inf;
rep(i,m)if(t[i]==2){
t[i]=0;
int t1=f(toi(t));
t[i]=1;
int t2=f(toi(t));
t[i]=2;
mi=min(mi,max(t1,t2)+1);
}
return dp[a]=mi;
}
int main(){
while(cin>>m>>n,n){
q=vi(m,2);
q[0]=0;
in=vs(n);
rep(i,n)cin>>in[i];
dp=vi(pow(3,m),inf);
cout<<f(toi(vi(m,2)))<<endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#define rep(i,n) for(int i = 0;i< n;i++)
#define INF 200
typedef long long int ll;
using namespace std;
int m,n;
int pow3[12];
int obj[200];
int dp[177150];//?????¢?´¢-1,?????¨?????????-2,?????????????????¢?´¢?????°
int fact(int num,int i){
return (num/pow3[i])%3;
}
void dfs(int que){
if(dp[que]!=-1)
return;
dp[que]=INF;
int left=0;
rep(i,n){ //??????????????§????????????????????????
int flag = 1;
rep(j,m){
int hoge = fact(que,j);
if(!hoge)
continue;
else{
hoge--;
if(hoge!=(obj[i]>>j&1)){
flag=0;
break;
}
}
}
left+=flag;
}
if(left == 1){
dp[que]=0;
return;
}else if(left == 0){
dp[que]= INF;
return;
}
rep(i,m){
if(!fact(que,i)){
int tempmax = -1;
rep(yn,2){
int next = que + (yn+1)*pow3[i];
dfs(next);
switch(dp[next]){
case -1:
break;
default:
tempmax=max(tempmax,dp[next]);
break;
}
}
dp[que]=min(dp[que],tempmax+1);
}
}
return;
};
int main(){
int temp = 1;
rep(i,12){
pow3[i]=temp;
temp *= 3;
}
while(cin >> m >> n&&m){
rep(i,pow3[m]){
dp[i]=-1;
}
rep(i,n){
obj[i]=0;
string tmp;
cin >> tmp;
rep(j,m){
obj[i]+= (tmp[j]-'0') << (m-1-j);
}
}
dfs(0);
cout << dp[0]<<endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
#define FOR(i,k,n) for(int i=(k);i<(int)(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) begin(x),end(x)
using namespace std;
int solve(map<vector<vector<bool>>, int>& memo, const vector<vector<bool>> &v, const set<int>& s) {
if (v.size() <= 1) return 0;
if (memo.find(v) != end(memo)) return memo[v];
int res = 11;
for (int i:s) {
vector<vector<bool>> a, b;
for (const auto& bs:v) {
if (bs[i]) a.push_back(bs);
else b.push_back(bs);
}
if (a.empty() || b.empty()) continue;
set<int> ns = s;
ns.erase(i);
int score = max(solve(memo, a, ns), solve(memo, b, ns)) + 1;
res = min(res, score);
}
memo[v] = res;
return res;
}
int main() {
while(1) {
int m,n;
cin>>m>>n;
if(!m) break;
vector<vector<bool>> vo(n, vector<bool>(m));
REP(i,n) {
string str;
cin>>str;
REP(j,m) vo[i][j] = str[j] == '1';
}
set<int> s;
REP(i,m) s.insert(i);
map<vector<vector<bool>>, int> memo;
int res = solve(memo, vo, s);
cout << res << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int) (n); i++)
#define reps(i, n) for (int i = 1; i <= (int) (n); i++)
#define all(x) (x).begin(), (x).end()
#define uniq(x) (x).erase(unique(all(x)), (x).end()))
#define bit(n) (1LL << (n))
#define cdiv(a, b) (((a) - 1) / (b) + 1)
#define dump(x) cerr << #x " = " << (x) << endl
using vint = vector<int>;
using vvint = vector<vint>;
using pint = pair<int, int>;
using vpint = vector<pint>;
template<typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
while (b) { swap(a %= b, b); }
return a;
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
template<typename T> void fin(T mes) {
cout << mes << endl;
exit(0);
}
template<typename T, typename U> bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<typename T, typename U> bool chmin(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
os << "(" << rhs.first << ", " << rhs.second << ")";
return os;
}
template<typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) {
os << "{";
for (auto itr = rhs.begin(); itr != rhs.end(); itr++) {
os << *itr << (next(itr) != rhs.end() ? ", " : "");
}
os << "}";
return os;
}
struct setup {
static constexpr int PREC = 20;
setup() {
cout << fixed << setprecision(PREC);
cerr << fixed << setprecision(PREC);
};
} setup;
signed main() {
while (true) {
int m, n;
cin >> m >> n;
if (m == 0 && n == 0) { break; }
vector<string> s(n);
rep(i, n) { cin >> s[i]; }
auto pow = [](int a, int n) {
int ret = 1;
rep(i, n) { ret *= a; }
return ret;
};
vint dp(pow(3, m));
for (int i = pow(3, m) - 1; i >= 0; i--) {
string state = "";
rep(j, m) { state.push_back(i / pow(3, j) % 3 + '0'); }
int cnt = 0;
rep(j, n) {
bool ok = true;
rep(k, m) {
if (s[j][k] == '0' && state[k] == '2') { ok = false; }
if (s[j][k] == '1' && state[k] == '1') { ok = false; }
}
if (ok) { cnt++; }
}
if (cnt <= 1) { continue; }
dp[i] = LLONG_MAX;
rep(j, m) {
if (state[j] != '0') { continue; }
int p = pow(3, j);
chmin(dp[i], max(dp[i + p * 1], dp[i + p * 2]) + 1);
}
}
cout << dp[0] << endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | from collections import Counter
import sys
def solve():
readline = sys.stdin.buffer.readline
write = sys.stdout.buffer.write
M, N = map(int, readline().split())
if M == N == 0:
return False
B = [int(readline(), 2) for i in range(N)]
memo = {}
def dfs(s, t):
key = (s, t)
if key in memo:
return memo[key]
c = 0
for b in B:
if (b & s) == t:
c += 1
if c <= 1:
memo[key] = 0
return 0
res = M
for i in range(M):
b = (1 << i)
if s & b == 0:
res = min(res, max(dfs(s|b, t), dfs(s|b, t|b))+1)
memo[key] = res
return res
write(b"%d\n" % dfs(0, 0))
return True
while solve():
...
| PYTHON3 |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <vector>
#include <unordered_map>
#include <bitset>
using namespace std;
typedef bitset<128> bit;
int M, N;
vector<string> v;
unordered_map<bit, int> memo;
int dfs(bit t){
if(memo.find(t) != memo.end())return memo[t];
if(t.count() == 1)return 0;
int r = 1 << 30;
for(int j=0;j<M;j++){
bit a, b;
for(int i=0;i<N;i++){
if(t[i]){
if(v[i][j] == '0'){
a.set(i);
} else {
b.set(i);
}
}
}
if(a.any() && b.any()){
r = min(r, max(dfs(a), dfs(b)) + 1);
}
}
return memo[t] = r;
}
int main(){
for(;cin>>M>>N,M;){
v.resize(N);
for(int i=0;i<N;i++)cin >> v[i];
bit t;
for(int i=0;i<N;i++)t.set(i);
memo.clear();
cout << dfs(t) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define rep(i,n) for(auto i=0*(n);i<(n);++i)
int main(){
while(true){
ll m, n;
cin >> m >> n;
if(m==0 && n == 0) break;
vector<string> a(n);
rep(i,n) cin >> a[i];
using P = bitset<128>;
vector<P> yes(m), no(m);
rep(i, m) rep(j, n) yes[i][j] = a[j][i] == '1';
rep(i, m) rep(j, n) no[i][j] = a[j][i] == '0';
map<ll, ll> cost;
function<ll(P, vector<int> &)> dfs = [&](P x, vector<int> & used) {
//cout << x.to_string() << endl;
//rep(i, m) cout << used[i] << " ";
//cout << endl;
if (x.count() <= 1) return 0ll;
ll id = 0; rep(i, n) { id *= 3; id += used[i]; }
if(cost.find(id) != cost.end()) return cost[id];
ll ans = 1e16;
rep(i, m) {
if (used[i] != 0) continue;
ll ma = 0;
used[i] = 1;
ma = max(ma, dfs(x & no[i], used));
used[i] = 2;
ma = max(ma, dfs(x & yes[i], used));
used[i] = 0;
ans = min(ans, ma);
}
assert(ans != 1e16);
return cost[id] = ans + 1;
};
P x; rep(i, n) x[i] = 1;
vector<int> used(m, 0);
cout << dfs(x, used) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <set>
#include <map>
using namespace std;
int M,N;
map<pair<set<int>,set<int>>,int> m;
char F[129][12];
int dfs(set<int> ob,set<int> fe){
if(ob.size()<=1) return (M-(int) fe.size());
if(m.count({ob,fe})) return m[{ob,fe}];
int res = 1e9;
set<int> fe2 = fe;
for(auto x:fe){
set<int> ob0,ob1;
for(auto y:ob){
if(F[y][x]=='0') ob0.insert(y);
else ob1.insert(y);
}
fe2.erase(x);
res = min(res,max(dfs(ob0,fe2),dfs(ob1,fe2)));
fe2.insert(x);
}
return m[{ob,fe}] = res;
}
int main(){
while(cin >> M >> N && N>0){
m.clear();
for(int i=0;i<N;i++) for(int j=0;j<M;j++) cin >> F[i][j];
set<int> ob,fe;
for(int i=0;i<N;i++) ob.insert(i);
for(int j=0;j<M;j++) fe.insert(j);
cout << dfs(ob,fe) << endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
template<class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
using ll = long long;
static const long long INF = 1e18;
using lll = __int128_t;
ll popcount(lll x) {
ll c = 0;
rep(i, 128)
c += !!(x & ((lll)1 << i));
return c;
}
int main(void) {
cin.tie(0); ios::sync_with_stdio(false);
ll n, qnum;
while (cin >> n >> qnum && n && qnum) {
vector<ll> a(qnum);
rep(i, qnum) rep(j, n) {
char c; cin >> c;
if (c - '0')
a[i] |= 1ll << j;
}
map<lll, ll> memo; // 1 if impossible
function<ll(lll, ll)> f = [&](lll x, ll counter) -> ll {
if (popcount(x) == qnum - 1)
return 0;
if (popcount(x) == qnum)
return INF;
if (memo.count(x))
return memo[x];
ll ret = INF;
rep(j, n) {
lll yes, no; yes = no = x;
rep(i, qnum)
(a[i] & ((ll)1 << j) ? yes : no) |= (lll)1 << i;
ll tmp = -1;
ll yes_ret = INF, no_ret = INF;
if (yes != x)
yes_ret = f(yes, counter+1);
if (no != x)
no_ret = f(no, counter+1);
if (yes_ret != INF)
chmax(tmp, yes_ret);
if (no_ret != INF)
chmax(tmp, no_ret);
if (tmp != -1)
chmin(ret, tmp + 1);
}
return memo[x] = ret;
};
cout << f(0, 0) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | /*
x日間悩み、
動的計画法でやろうということを思いついた。
数学的に解が得られる方法は複数ありうる。
しかし、0 < m \leq 11 and 0 < n \leq 128 という制約の中で
制限に引っかからなそうなものとなると、状態の持ち方が限られる。
たとえば自然な発想として、
「n個のonjectの各部分集合を状態として持ち、それを類別するために必要な最小の
質問回数」を配列で持とうとするものが考えられる。
しかし2^{128}個のintはメモリの上においておくのは無理である。
よく考えてみると相当無駄がある。質問はm \leq 11個しかない
だから「出てきうる」部分集合の個数は、せいぜい3^{11}個であり、2^{128}より
はるかに少ない。
そこで、
「m個の質問それぞれを『未回答』『はい』『いいえ』をみたす
onjectの集合を状態として持ち、それを類別するために必要な最小の
質問回数」を配列で持とう。これなら3^{11}個である。
(ただし以下の実装の2次元配列のサイズは2^{11} \times 2^{11}である)
次なる問題はどのようにしてこの動的計画法を進めるかである。
ここでさらにy日悩んだが、
「集合の元の数が1個か0個」は0とし、
あとは深さ優先探索を併用している。
完全に再帰でやるとメモリ足りないかもしれないので、
『未回答』が少ない集合からまわしていき、
あまり深すぎないように努力した。
*/
#include <iostream>
#include <vector>
#include <cassert>
#include <string>
using namespace std;
vector< vector<int> > V; // [0:未回答 1:回答済み][0:Yes 1:No]
int n, m, C;
vector<int> W;
const int infty = 1000;
vector< vector<int> > X; // X[i] = i個bitが立っている集合
void init() {
C = (1 << m);
V.clear();
V = vector< vector<int> >(C, vector<int>(C, infty));
W.clear();
for (auto i=0; i<n; i++) {
long long w;
string s;
cin >> s;
w = stoll(s);
int x = 0;
int y = 1;
for (auto j=0; j<m; j++) {
x += w%10 * y;
y *= 2;
w /= 10;
}
// cerr << x << endl;
W.push_back(x);
}
X.clear();
X = vector< vector<int> >(m+1, vector<int>(0));
for (auto i=0; i<C; i++) {
int count = 0;
for (auto k=0; k<m; k++) {
if ((i >> k) & 1) {
count++;
}
}
X[count].push_back(i);
// cerr << "X[" << count << "] pushed " << i << endl;
}
}
bool ok(int i, int j) { // 未回答なのにjに1が立っているものを弾く
for (auto k=0; k<m; k++) {
if ( ((j >> k) & 1) && (((i >> k) & 1) == 0) ) {
return false;
}
}
return true;
}
void costzero() {
for(auto i=0; i<C; i++) {
for (auto j=0; j<C; j++) {
if (ok(i, j)) {
// cerr << "i = " << i << ", j = " << j << endl;
vector<int> K = W;
for (auto k=0; k<m; k++) {
if ((i >> k) & 1) {
vector<int> L;
int x = (j >> k) & 1;
for (unsigned int l=0; l<K.size(); l++) {
if (((K[l] >> k) & 1) == x) {
L.push_back(K[l]);
}
}
K.clear();
K = L;
}
}
// cerr << K.size() << endl;
if (K.size() <= 1) {
V[i][j] = 0;
// cerr << "i = " << i << ", j = " << j << endl;
}
}
}
}
}
int cost(int i, int j) {
if (V[i][j] < infty) {
return V[i][j];
}
int ans = infty;
for (auto k=0; k<m; k++) {
if (((i >> k) & 1) == 0) {
int ansk = 0;
int ni = i | (1 << k);
for (auto l=0; l<2; l++) {
int nj = j | (l << k);
int anskl = cost(ni, nj);
if (ansk < anskl) ansk = anskl;
}
if (ans > ansk) ans = ansk;
}
}
if (ans == infty) {
cerr << "infty i = " << i << " j = " << j << endl;
}
assert(ans < infty);
ans++;
V[i][j] = ans;
return ans;
}
int main() {
while(cin >> m >> n && m) {
init();
costzero();
for (int i=m; i>=0; i--) {
for (unsigned int j=0; j<X[i].size(); j++) {
for (auto k=0; k<C; k++) {
if (ok(X[i][j], k)) {
cost(X[i][j], k);
}
}
}
}
cout << cost(0, 0) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)(n); i++)
#define FOR(i,a,b) for(int i = (a); i < (int)(b); i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pi;
typedef long long ll;
typedef unsigned long long ull;
const int IINF = 1<<28;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
map<vs, int> m2;
int dfs(vs &v, int m, int mask) {
if(v.size() < 2)
return v.size()-1;
if(m2.count(v))
return m2[v];
int ans = IINF;
REP(i, m) {
vs a, b;
int mask2 = mask;
if(!(mask2 >> i & 1)) {
mask2 |= 1 << i;
REP(j, v.size()) {
if(v[j][v[j].size()-1-i] == '1')
a.pb(v[j]);
else
b.pb(v[j]);
}
int res = max(dfs(b, m, mask2), dfs(a, m, mask2))+1;
ans = min(ans, res);
}
}
m2[v] = ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int m, n;
while(cin >> m >> n, m|n) {
m2.clear();
vs v(n);
REP(i, n)
cin >> v[i];
cout << dfs(v, m, 0) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
const long long int MOD = 1000000007;
const int INF = 1000000000;
int n, m;
int x[128][11];
map<string,int> dp;
int dfs(string str){
//cout << str << endl;
if(dp.count(str)){
return dp[str];
}
int cnt = 0;
rep(i,n){
if(str[i]=='1') cnt++;
}
if(cnt == 1){
return 0;
}
int ret = INF;
rep(i,m){
string a, b;
bool oka = false, okb = false;
rep(j,n){
if(str[j] == '0'){
a.push_back('0');
b.push_back('0');
} else{
if(x[j][i]){
a.push_back('1');
b.push_back('0');
oka = true;
} else{
a.push_back('0');
b.push_back('1');
okb = true;
}
}
}
if(oka == false || okb == false) continue;
ret = min(ret,max(dfs(a),dfs(b))+1);
}
//cout << ret << endl;
dp[str] = ret;
return ret;
}
void solve(){
dp.clear();
rep(i,n) rep(j,m){
char hoge;
cin >> hoge;
if(hoge == '1') x[i][j] = 1;
else x[i][j] = 0;
}
if(n == 1){
cout << 0 << endl;
return;
}
string hoge;
rep(i,n) hoge.push_back('1');
cout << dfs(hoge) << endl;
}
int main(){
while(cin >> m >> n){
if(n == 0 && m == 0) break;
solve();
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
using namespace std;
const int INF=1000;
int powpow(int x,int k){
int res=1;
for(int i=0;i<k;i++) res*=x;
return res;
}
int solve(int m,int n){
vector<vector<int>> obj(n,vector<int>(m));
for(int i=0;i<n;i++){
string s;
cin>>s;
for(int j=0;j<m;j++){
obj[i][j]=s[j]-'0';
}
}
int pow3=powpow(3,m);
vector<int> dp(pow3,INF);
for(int bit=pow3-1;bit>=0;bit--){
vector<int> st(m);
int x=bit;
for(int i=0;i<m;i++){
st[i]=x%3;
x/=3;
}
vector<int> idx;
for(int i=0;i<n;i++){
bool contain=true;
for(int j=0;j<m;j++){
contain=contain && (st[j]==0 || st[j]-1==obj[i][j]);
}
if(contain){
idx.push_back(i);
}
}
if(idx.size()<=1){
dp[bit]=0;
}
else{
for(int i=0;i<m;i++){
if(!st[i]){
dp[bit]=min(dp[bit],max(dp[bit+powpow(3,i)],dp[bit+2*powpow(3,i)])+1);
}
}
}
}
return dp[0];
}
int main(){
int n,m;
while(cin>>m>>n,m){
cout<<solve(m,n)<<endl;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | from collections import Counter
while True:
m, n = (int(s) for s in input().split())
if not m:
break
objects = [int(input(), 2) for i in range(n)]
dp = [bytearray(1 << m) for i in range(1 << m)]
bits = [1 << i for i in range(m)]
for mask in reversed(range((1 << m) - 1)):
for masked, count in Counter(obj & mask for obj in objects).items():
if count > 1:
dp[mask][masked] = min(max(dp[mask + b][masked],
dp[mask + b][masked + b]) + 1
for b in bits if not b & mask)
print(dp[0][0]) | PYTHON3 |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
const int INF = 1e9;
ll M, N;
vector<string> S;
map<vector<int>, int> dp;
int f(vector<int> v) {
if(dp.count(v)) return dp[v];
int cnt = 0;
for(int i = 0; i < N; i++) {
bool ok = true;
for(int j = 0; j < M; j++) {
if(v[j] == 1 and S[i][j] == '0') ok = false;
if(v[j] == 2 and S[i][j] == '1') ok = false;
}
if(ok) cnt++;
}
if(cnt == 0) return dp[v] = -1e9;
if(cnt == 1) return dp[v] = 0;
int ret = 1e9;
for(int q = 0; q < M; q++) {
if(v[q] != 0) continue;
v[q] = 1;
int tmp1 = f(v);
v[q] = 2;
int tmp2 = f(v);
v[q] = 0;
chmin(ret, max(tmp1, tmp2));
}
return dp[v] = ret + 1;
}
void solve() {
S.resize(N);
for(int i = 0; i < N; i++) cin >> S[i];
dp.clear();
vector<int> v;
v.resize(M, 0);
cout << f(v) << endl;
}
int main() {
while(cin >> M >> N) {
if(M == 0) break;
solve();
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
using namespace std;
#define int long long
map<int,int> dp[1<<12];
int m,n;
string s[200];
int dfs(int b,int r){
if(dp[b].count(r)) return dp[b][r];
int res=m;
int cnt=0;
for(int i=0;i<n;i++){
int f=1;
for(int j=0;j<m;j++)
if(b>>j&1) f&=((s[i][j]=='1')==(r>>j&1));
cnt+=f;
}
if(cnt<=1) return 0;
for(int i=0;i<m;i++)
if(!(b>>i&1)) res=min(res,max(dfs(b+(1<<i),r+(1<<i)),
dfs(b+(1<<i),r))+1);
return dp[b][r]=res;
}
signed main(){
while(cin>>m>>n,m||n){
for(int i=0;i<n;i++) cin>>s[i];
for(int i=0;i<(1<<m);i++) dp[i].clear();
cout<<dfs(0,0)<<endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int hoge(int x, int y)
{
int r = 1;
for(int i = 0; i < y; ++i)
r *= x;
return r;
}
int calc(int state, int m, vector<int> &a, vector<int> &memo)
{
if(memo[state] != -1)
return memo[state];
int n = a.size();
int ans = 0, filter = 0, s = state;
for(int i = 0; i < m; ++i) {
int x = s % 3;
if(x != 0) filter |= 1 << i;
if(x == 2) ans |= 1 << i;
s = s / 3;
}
int count = 0;
for(int i = 0; i < n; ++i) {
if((a[i] ^ ans) & filter)
continue;
count += 1;
}
if(count <= 1)
return memo[state] = 0;
int ret = 1000;
for(int i = 0; i < m; ++i) {
if((filter >> i) & 1)
continue;
int p3i = hoge(3, i);
int lv = calc(state + p3i * 1, m, a, memo) + 1;
int rv = calc(state + p3i * 2, m, a, memo) + 1;
ret = min(ret, max(lv, rv));
}
return memo[state] = ret;
}
int main()
{
while(true) {
int m, n;
vector<int> a;
vector<int> memo;
scanf("%d%d", &m, &n);
if(m == 0)
break;
a.resize(n);
for(int i = 0; i < n; ++i) {
char temp[64];
scanf("%s", temp);
int &v = a[i];
for(int j = 0; j < m; ++j)
v |= (temp[j] == '1') << j;
}
memo.resize(hoge(3, m), -1);
int ans = calc(0, m, a, memo);
printf("%d\n", ans);
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
#define ll long long
#define INF (1LL << 63)
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
using namespace std;
typedef __int128 LL;
typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
const int MAX_N = 100005;
map<LL,int> mp;
int n,m;
vs s;
ostream& operator<<(ostream& os, const LL& v)
{
if (v == 0) { return (os << "0"); }
LL num = v;
if (v < 0) {
os << '-';
num = -num;
}
string s;
for (; num > 0; num /= 10) { s.push_back((char)(num % 10) + '0'); }
reverse(s.begin(), s.end());
return (os << s);
}
inline int dfs(int nw,LL p)
{
if(mp.find(p) != mp.end()) return mp[p];
int ct = 0;
rep(i,n){
if(p >> i & (LL)1){
ct++;
}
}
if(ct == 1) return 0;
int res = m+1;
rep(id,m){
if(!(nw >> id & 1)){
LL a = 0, b = 0;
rep(i,n){
if(p >> i & (LL)1){
if(s[i][id] & 1){
a |= (LL)1 << i;
}else{
b |= (LL)1 << i;
}
}
}
if(a != 0 && b != 0){
cmn(res,max(dfs(nw^(1 << id),a),dfs(nw^(1 << id),b))+1);
}
}
}
mp[p] = res;
return res;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
while(1){
cin >> m >> n;
if(m == 0) break;
s.resize(n);
mp.clear();
rep(i,n){
cin >> s[i];
}
LL hoge = 0;
rep(i,n){
hoge |= (LL)1 << i;
}
cout << dfs(0,hoge) << "\n";
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const int MAX = INT_MAX / 10;
int m, n;
int get_hash(vi &v) {
int n = v.size();
int ret = 0;
REP(i,n) {
ret *= 3;
ret += v[i];
}
return ret;
}
int dfs(vs &o, vi &v, vi &memo) {
int hash = get_hash(v);
if (memo[hash] != -1) {
return memo[hash];
}
/*
cerr << "v:" << endl;
REP(i,m) {
cerr << v[i];
}
cerr << endl;
*/
int cnt = 0;
vi ok(n, 1);
REP(i,m) {
// cerr << i << " --> " << endl;
if (v[i] != 2) {
cnt++;
REP(j,n) {
// cerr << (v[i] ^ (o[j][i] - '0'));
if ((v[i] ^ (o[j][i] - '0')) == 1) {
ok[j] = 0;
}
}
// cerr << endl;
}
}
int ok_cnt = 0;
// cerr << "ok:" << endl;
REP(i,n) {
// cerr << ok[i] << endl;
if (ok[i]) {
ok_cnt++;
}
}
if (ok_cnt == 1) {
/*
if (cnt == 1) {
cerr << "AAAAA" << endl;
REP(i,m) {
cerr << v[i];
}
cerr << "," << ok_cnt << endl;
}
*/
return cnt;
}
int ret = MAX;
REP(i,m) {
if (v[i] == 2) {
int tmp = 0;
REP(j,2) {
vi nv(v);
nv[i] = j;
tmp = max(tmp, dfs(o, nv, memo));
}
ret = min(ret, tmp);
}
}
return memo[hash] = ret;
}
int main() {
while (cin >> m >> n, m | n) {
vs o(n);
REP(i,n) {
cin >> o[i];
}
vi v(m, 2);
vi memo(get_hash(v) + 1, -1);
cout << dfs(o, v, memo) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> vi;
int m,n;
string obj[130];
map<vi, int> mp;
int dfs(vi &c){
if(c.size()<=1) return 0; //分かった
int& x = mp[c];
if(x) return x; //探索済み。その他、デフォは0
x = m;
for(int i=0;i<m;++i){
vi l,r;
for(int j : c){
if(obj[j][i]=='1') l.push_back(j);
else r.push_back(j);
}
if(l.empty() || r.empty()) continue;
x = min(x, 1+max(dfs(l),dfs(r)));
}
return x;
}
int main(){
while(cin>>m>>n){
if(m==0 && n==0) return 0;
mp.clear();
for(int j=1;j<=n;++j) cin>>obj[j];
vi c; //cand
for(int j=1;j<=n;++j) c.push_back(j);
cout << dfs(c) << endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int w;
map<vector<int>,int> mem;
int dfs(vector<int> &A){
int n = A.size();
if(n<=1) return 0;
if(mem.count(A)) return mem[A];
int res = A.size()-1;
for(int i=0;i<w;i++){
vector<int> T,F;
for(int j=0;j<n;j++) (A[j]>>i&1)? T.push_back(A[j]) : F.push_back(A[j]);
if(T.empty() || F.empty()) continue;
res = min(res,max(dfs(T),dfs(F))+1);
}
return mem[A] = res;
}
int BtoI(string a){
int res = 0;
for(int i=0;i<(int)a.size();i++) res = res*2+a[i]-'0';
return res;
}
int main(){
while(1){
int n;
cin>>w>>n;
if(w==0&&n==0)break;
vector<int> A;
A.resize(n);
for(int i=0;i<n;i++){
string s;
cin>>s;
A[i] = BtoI(s);
}
mem.clear();
cout<<dfs(A)<<endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;
int m, n;
vector<bitset<11> > bs;
vector<int> memo;
int solve(int a)
{
if(memo[a] != -1)
return memo[a];
bitset<11> zero, one;
zero = a & ((1<<m)-1);
one = a >> m;
int num = 0;
for(int i=0; i<n; ++i){
if((bs[i] & zero).none() && (~bs[i] & one).none())
++ num;
}
if(num <= 1)
return memo[a] = 0;
int ret = INT_MAX;
for(int i=0; i<m; ++i){
if(zero[i] || one[i])
continue;
zero[i] = true;
int tmp = solve(zero.to_ulong() + (one.to_ulong() << m));
zero[i] = false;
one[i] = true;
tmp = max(tmp, solve(zero.to_ulong() + (one.to_ulong() << m)));
one[i] = false;
ret = min(ret, tmp + 1);
}
return memo[a] = ret;
}
int main()
{
for(;;){
cin >> m >> n;
if(m == 0)
return 0;
bs.resize(n);
for(int i=0; i<n; ++i){
string s;
cin >> s;
bs[i] = bitset<11>(s);
}
memo.assign(1<<(2*m), -1);
cout << solve(0) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#define INF 1<<10
using namespace std;
int n,m;
int dp[1<<12][1<<12];
int data[150];
vector<int> dif;
int memo(int used,int yn){
if(dp[used][yn]!=INF)return dp[used][yn];
int cnt=0;
for(int i=0;i<n;i++){
if((used&data[i])==yn)cnt++;
}
if(cnt<=1)return dp[used][yn]=0;
int res=INF;
for(int i=0;i<m;i++){
if(used>>i & 1)continue;
res=min(res,max(memo(used|(1<<i),yn),memo(used|(1<<i),yn|(1<<i))));
}
return dp[used][yn]=(res+1);
}
int main(void){
while(1){
scanf("%d%d",&m,&n);
if(n+m==0)break;
memset(data,0,sizeof(data));
for(int i=0;i<n;i++){
string str;
cin >> str;
for(int j=0;j<m;j++){
if(str[j]=='1')data[i]+=(1<<j);
}
}
for(int i=0;i<(1<<m);i++){
for(int j=0;j<(1<<m);j++){
dp[i][j]=INF;
}
}
printf("%d\n",memo(0,0));
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
const int inf = 1e9;
bool issame(string &a, string &b){
for(int i=0; i<(int)a.length(); i++){
if(a[i]!='x' && b[i]!='x' && a[i]!=b[i]){
return false;
}
}
return true;
}
int solve(string &str, vector<string> &s, map<string, int> &mincost){
if(mincost.count(str) != 0){
return mincost[str];
}
int n = s.size();
int count = 0;
for(int i=0; i<n; i++){
if(issame(s[i], str)) count++;
}
if(count <= 1){
mincost[str] = 0;
return 0;
}
int m = str.length();
int res = inf;
for(int i=0; i<m; i++){
if(str[i] == 'x'){
string tmpstr[2] = {str, str};
tmpstr[0][i] = '0';
tmpstr[1][i] = '1';
res = min(res, max(solve(tmpstr[0], s, mincost), solve(tmpstr[1], s, mincost)) +1);
}
}
mincost[str] = res;
return res;
}
int main(){
while(1){
int m,n;
cin >> m >> n;
if(m==0) break;
vector<string> s(n);
for(int i=0; i<n; i++) cin >> s[i];
map<string, int> mincost;
string ini = string(m, 'x');
cout << solve(ini, s, mincost) << endl;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0,i##_cond=(n);i<i##_cond;++i)
const int inf = 1<<30;
using vs = vector<string>;
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
int n, m;
vs info;
map<string, int> memo;
int rec(string s){
int c = 0;
rep(i,m) if(s[i] == '1') c++;
if(c <= 1) return 0;
if(memo.count(s)) return memo[s];
int res = inf;
rep(i,n){ // i番目の質問をするとき
string zero = s, one = s;
rep(j,m){ // j番目のもの
if(s[j] == '0') continue; // もう区別されている
if(info[j][i] == '0') one[j] = 0;
else zero[j] = 0;
}
if(zero == s or one == s) continue;
int tmp = max(rec(zero), rec(one));
chmin(res, tmp);
}
memo[s] = res+1;
return res+1;
}
int main(){
while(cin >> n >> m, n){
info.resize(m);
rep(i,m) cin >> info[i];
memo.clear();
string all;
rep(i,m) all += '1';
cout << rec(all) << endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int n;
int m;
int lim;
int ans;
int as[1145];
map<vector<int>, int> memo;
int dfs(vector<int> &vi) {
int ret = m;
if (memo.count(vi)) return memo[vi];
if (vi.size() <= 1) return 0;
for (int i=0; i<m; i++) {
vector<int> a, b;
for (int idx : vi) {
if (as[idx] >> i & 1) a.emplace_back(idx);
else b.emplace_back(idx);
}
if (a.empty() || b.empty()) continue;
ret = min(ret, max(dfs(a), dfs(b))+1);
}
return memo[vi] = ret;
}
int main() {
while (1) {
scanf("%d%d", &m, &n);
if ((m|n) == 0) return 0;
memo.clear();
vector<int> al;
for (int i=0; i<n; i++) {
al.emplace_back(i);
as[i] = 0;
for (int j=0; j<m; j++) {
int t;
scanf("%01d", &t);
as[i] = (as[i] << 1) | t;
}
}
printf("%d\n", dfs(al));
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | //
// main.cpp
// H
//
// Created by 譚?髣サ霎セ on 15/2/24.
// Copyright (c) 2015蟷エ 譚?髣サ霎セ. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
int n,m,a,b[130],dp[2100][2100],v[15];
int dfs(int s1,int s2){
if(dp[s1][s2]!=dp[2099][2099])return dp[s1][s2];
int cnt=0;
for(int i=0;i<m;i++)
if((b[i]&s1)==s2)cnt++;
if(cnt<=1)dp[s1][s2]=0;
else{
for(int i=0;i<n;i++)
if(!(s1&v[i]))
dp[s1][s2]=std::min(dp[s1][s2],std::max(dfs(s1|v[i],s2),dfs(s1|v[i],s2^v[i]))+1);
}
return dp[s1][s2];
}
int main(int argc, const char * argv[]) {
for(int i=0;i<15;i++)v[i]=1<<i;
while(scanf("%d%d",&n,&m)){
if(!n && !m)break;
memset(dp,10,sizeof(dp));
for(int i=0;i<m;i++){
b[i]=0;
for(int j=0;j<n;j++){
scanf("%1d",&a);
if(a==1)b[i]|=v[j];
}
}
printf("%d\n",dfs(0,0));
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | // Enjoy your stay.
#include <bits/stdc++.h>
#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++)
#define ite iterator
#define mp make_pair
#define mt make_tuple
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())
using namespace std;
typedef istringstream iss;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef stringstream sst;
typedef vector<ll> vi;
int m,n,obj[200];
int dp[180000];
int p3[12];
int f(int mask){
int& res = dp[mask];
if(res!=-1)return res;
res = INF;
int d[12];
rep(i,m)d[i] = mask/p3[i]%3;
int hit = 0;
rep(i,n){
int ok = 1;
rep(j,m)if(d[j]!=2 && d[j] != (obj[i]>>j&1)){
ok = 0;break;
}
if(ok)hit++;
}
if(hit <= 1)return res = 0;
rep(i,m)if(d[i] == 2){
res = min(res, 1 + max(f(mask - 2 * p3[i]), f(mask - 1 * p3[i])));
}
return res;
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
p3[0] = 1;
rep(i,11)p3[i+1] = p3[i] * 3;
while(cin>>m>>n && m){
rep(i,n){
string s;
cin>>s;
obj[i] = 0;
rep(j,m)obj[i] |= (s[j] - '0')<<j;
}
memset(dp,-1,sizeof(dp));
cout<<f(p3[m]-1)<<endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int func(vector<bitset<11>> &v, unordered_map<bitset<128>, int> &mp, bitset<128> x, int n, int m){
if(mp.count(x)) return mp[x];
if(x.count() == 1) return mp[x] = 0;
int res = 1e9;
for(int i=0; i<m; ++i){
bitset<128> y = x, z = x;
for(int j=0; j<n; ++j){
if(v[j][i] == 1) z[j] = 0;
else y[j] = 0;
}
if(y.count() == 0 || z.count() == 0) continue;
// cout << i << " " << y << " " << z << "\n";
int t = 0;
t = func(v, mp, y, n, m);
t = max(t, func(v, mp, z, n, m));
res = min(res, t + 1);
}
return mp[x] = res;
}
int solve(int m, int n){
vector<bitset<11>> v;
unordered_map<bitset<128>, int> mp;
for(int i=0; i<n; ++i){
string s;
cin >> s;
v.push_back(bitset<11>(s));
}
return func(v, mp, bitset<128>(string(n, '1')), n, m);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int m, n;
while(1){
cin >> m >> n;
if(m == 0) return 0;
cout << solve(m, n) << "\n";
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <cstring>
#include <climits>
using namespace std;
int w, h;
short dp[1<<11][1<<11];
int bit[200];
bool t[200][200];
int solve(int q, int a){
if(dp[q][a] != -1) return dp[q][a];
int cnt = 0;
for(int i = 0; i < h; i++){
if((bit[i] & q) == a){
cnt++;
if(cnt > 1) break;
}
}
if(cnt == 1){
return 0;
}
int res = INT_MAX;
for(int i = 0; i < w; i++){
if(!(q & (1 << i))){
int tmp = max(solve(q | (1 << i), a),
solve(q | (1 << i), a | (1 << i)));
res = min(res, tmp);
}
}
return dp[q][a] = res + 1;
}
int main(){
while(cin >> w >> h, w || h){
for(int i = 0; i < h; i++){
bit[i] = 0;
for(int j = 0; j < w; j++){
char ch;
cin >> ch;
t[i][j] = ch - '0';
bit[i] = (bit[i] << 1) + t[i][j];
}
}
memset(dp, -1, sizeof(dp));
cout << solve(0, 0) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <array>
#include <algorithm>
#include <bitset>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }
constexpr int MAX_M = 11;
int m, n;
int memo[1 << MAX_M][1 << MAX_M];
int dfs(int used, int result, const vector<unsigned> &objects) {
if(objects.size() <= 1) return 0;
int &res = memo[used][result];
if(res != -1) return res;
res = MAX_M;
for(int i = 0; i < m; ++i) {
if((used >> i) & 1) continue;
array<vector<unsigned>, 2> next_objects;
for(const auto &e : objects) {
next_objects[(e >> i) & 1].emplace_back(e);
}
const int yes = dfs(used | (1 << i), result | (1 << i), next_objects[1]);
const int no = dfs(used | (1 << i), result, next_objects[0]);
chmin(res, max(yes, no) + 1);
}
return res;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
while(cin >> m >> n && m) {
vector<unsigned> objects(n);
for(auto &e : objects) {
bitset<MAX_M> b;
cin >> b;
e = b.to_ulong();
}
memset(memo, -1, sizeof(memo));
cout << dfs(0, 0, objects) << endl;
}
return EXIT_SUCCESS;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int M, N;
vector<int> objects;
int memo[1<<11][1<<11];
int to_int(string s){
int res = 0;
for(int i=0; i<s.size(); i++){
res = res*2 + (s[i] == '1');
}
return res;
}
int dfs(int s, int ys){
if(memo[s][ys] >= 0) return memo[s][ys];
int& res = memo[s][ys];
int cnt = 0;
for(auto v: objects) if((v&s) == ys) cnt++;
if(cnt < 2)
return res = 0;
res = M;
for(int i=0; i<M; i++) if((s>>i&1) == 0){
res = min(res, max(dfs(s|(1<<i), ys)+1, dfs(s|(1<<i), ys|(1<<i))+1));
}
return res;
}
void solve(){
memset(memo, -1, sizeof(memo));
cout << dfs(0, 0) << endl;
}
int main(){
while(cin >> M >> N, M|N){
objects.resize(N);
string s;
for(int i=0; i<N; i++){
cin >> s; objects[i] = to_int(s);
}
solve();
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | import java.util.*;
public class Main {
static int minmin;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int m = sc.nextInt();
int n = sc.nextInt();
if (m == 0 && n == 0) break;
List<boolean[]> data = new ArrayList<>();
boolean[][] dset = new boolean[n][m];
for (int i = 0; i < n; i++) {
String s = sc.next();
boolean[] d = new boolean[m];
for (int j = 0; j < m; j++) {
if (s.charAt(j) == '1')
d[j] = true;
else d[j] = false;
dset[i][j]=d[j];
}
data.add(d);
}
minmin = min_min(m,n,dset);
System.out.println(solve(data,0,m));
}
}
private static int solve(List<boolean[]> data,int time,int size){
if (data.size() == 1) return 0;
if (data.size() == 2) return 1;
if (data.size() == 3) return 2;
int o=0,z=0,min=12;
for(int i=0;i<size;i++){
List<boolean[]> zero = new ArrayList<>();
List<boolean[]> one = new ArrayList<>();
for(boolean[] a:data){
if(a[i])one.add(a);
else zero.add(a);
}
if(one.size()==0||zero.size()==0)continue;
o = solve(one,time+1,size);
z = solve(zero,time+1,size);
if(o>=z&&o<min)min=o;
else if(z>o&&z<min)min=z;
if(minmin-1==min+time)break;
}
return min+1;
}
public static int min_min(int m, int n, boolean[][] dset) {
List<boolean[]> list = makelist(m);
int max = 0;
loop1:
for (int i = 0; i < n; i++) {
int ko;
loop2:
for (int a = 0; a < list.size(); a++) {
boolean[] set = list.get(a);
loop3:
for (int j = 0; j < n; j++) {
if (i == j) continue;
loop4:
for (int k = 0; k < m; k++) {
if (!set[k]) continue;
if (dset[i][k] != dset[j][k]) continue loop3;
}
continue loop2;
}
/*
for (int p = 0; p < m; p++) {
if (!set[p]) continue;
if (dset[i][p]) System.out.print(1+"("+p+")");
else System.out.print(0+"("+p+")");
}
System.out.println("-------");
for (int q = 0; q < n; q++) {
if(q==i)continue;
for (int p = 0; p < m; p++) {
if (!set[p]) continue;
if (dset[q][p]) System.out.print(1);
else System.out.print(0);
}
System.out.print("\n");
}
System.out.println("");
*/
ko = count(set);
if (ko > max) max = ko;
break;
}
}
return max;
}
private static List<boolean[]> makelist(int m) {
Map<Integer, boolean[]> map = new HashMap<>();
Queue<boolean[]> que = new ArrayDeque<>();
boolean[] a = new boolean[m];
for (boolean b : a) b = false;
que.add(a.clone());
map.put(getkey(a), a.clone());
while (!que.isEmpty()) {
boolean[] b = que.remove();
for (int i = 0; i < m; i++) {
boolean[] c = b.clone();
if (!c[i]) {
c[i] = true;
int key = getkey(c);
if (!map.containsKey(key)) {
que.add(c);
map.put(key, c);
}
}
}
}
List<boolean[]> list = new ArrayList<>(map.values());
Collections.sort(list,
new Comparator<boolean[]>() {
@Override
public int compare(boolean[] o1, boolean[] o2) {
return count(o1) - count(o2);
}
}
);
return list;
}
private static int getkey(boolean[] a) {
int key = 0, k = 1;
for (int i = 0; i < a.length; i++) {
if (a[i]) key += k;
k *= 10;
}
return key;
}
private static int count(boolean[] be) {
int count = 0;
for (boolean b : be) {
if (b) count++;
}
return count;
}
}
| JAVA |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
//const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int m, n;
int f[135][15];
map<vector<int>, int> mem;
int dfs(vector<int>& S) {
if (S.size() <= 1) return 0;
if (mem[S]) return mem[S];
int res = m;
rep(i, m) {
vector<int> left, right;
for (int v: S) {
if (f[v][i]) left.push_back(v);
else right.push_back(v);
}
if (left.empty() || right.empty()) continue;
chmin(res, 1 + max(dfs(left), dfs(right)));
}
return mem[S] = res;
}
void solve() {
mem.clear();
vector<int> a;
rep(i, n) a.push_back(i);
cout << dfs(a) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(25);
while (cin >> m >> n, m && n) {
rep(i, n) {
string s;
cin >> s;
rep(j, m) f[i][j] = s[j] - '0';
}
solve();
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
/*--------------------template--------------------*/
vi v;
int m, n;
short dp[1 << 12][1 << 12];
int solve(int asked, int resp)
{
if (dp[asked][resp] >= 0) return dp[asked][resp];
int cnt = 0;
REP(i, n)
{
if ((v[i] & asked) == resp) cnt++;
}
int res = m;
if (cnt <= 1)
{
res = 0;
}
else
{
REP(i, m)
{
int nx = asked | (1 << i);
if (nx == asked) continue;
chmin(res, max(solve(nx, resp | (1 << i)), solve(nx, resp))+1);
}
}
return dp[asked][resp] = res;
}
int tonum(string s)
{
int res = 0;
REP(i, s.size())
{
res *= 2;
res += s[i] - '0';
}
return res;
}
int main()
{
while (cin >> m >> n, m)
{
MS(dp, -1);
v.resize(n);
REP(i, n)
{
string s;
cin >> s;
v[i] = tonum(s);
}
cout << solve(0, 0) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
map< vector< int >, int > dp;
int M, N;
int bit[128];
int rec(vector< int > idx, int v)
{
if(dp.count(idx)) return (dp[idx]);
if(idx.size() <= 1) return (0);
int ret = 1333;
for(int i = 0; i < M; i++) {
if((v >> i) & 1) continue;
vector< int > latte, malta;
for(int j : idx) ((bit[j] >> i) & 1 ? latte : malta).push_back(j);
ret = min(ret, max(rec(latte, v | (1 << i)), rec(malta, v | (1 << i))) + 1);
}
return (dp[idx] = ret);
}
int main()
{
while(cin >> M >> N, M) {
for(int i = 0; i < N; i++) {
string S;
cin >> S;
bit[i] = 0;
for(int j = 0; j < M; j++) bit[i] |= (S[j] == '1') << j;
}
vector< int > idx(N);
for(int i = 0; i < N; i++) idx[i] = i;
dp.clear();
cout << rec(idx, 0) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
using namespace std;
int M,N;
char s[135][15];
map < pair< vector<int>,int > ,int> MAP;
int flere(vector <int> list,int level,int used){
int i,j,k=M;
if(MAP.find(make_pair(list,used))!=MAP.end())
return MAP[make_pair(list,used)];
if(list.size()==1)
return level;
for(i=0;i<M;i++){
if(used & (1<<i))
continue;
vector <int> a,b;
for(j=0;j<list.size();j++){
if(s[list[j]][i]=='0')
a.push_back(list[j]);
else
b.push_back(list[j]);
}
if(a.size()==0 || b.size()==0)
continue;
k = min(k,max(flere(a,level+1,used|(1<<i)),flere(b,level+1,used|(1<<i))));
}
return MAP[make_pair(list,used)] = k;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m,i,j;
while(~scanf(" %d %d",&M,&N) && (M+N)){
gets(s[0]);
for(n=0;n<N;n++)
gets(s[n]);
vector <int> list;
MAP.clear();
for(n=0;n<N;n++)
list.push_back(n);
printf("%d\n",flere(list,0,0));
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int m, n;
int ans[150];
int dp[2050][2050]; // S?????????????????£??????
using Set = bitset<12>;
int rec(int asked, int answer){ // ??????????????????aked???answer??¨??????????????????
int &res = dp[asked][answer];
if(res != -1) return res;
res = m;
int rem = 0;
for(int i = 0; i < n; ++i){
if((ans[i] & asked) == answer){
++rem;
}
}
if(rem <= 1) return res = 0;
for(int i = 0; i < m; ++i){
if(asked >> i & 1) continue;
int cnt[2] = {};
for(int j = 0; j < n; ++j){
if((ans[j] & asked) == answer){
++cnt[ans[i] >> j & 1];
}
}
// if(cnt[0] == 0 || cnt[1] == 0) continue;
int tmp = 0;
for(int j = 0; j < 2; ++j){
tmp = max(tmp, 1 + rec(asked | 1<<i, answer | (j << i)));
}
res = min(res, tmp);
}
return res;
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
while(cin >> m >> n && m){
for(int i = 0; i < n; ++i){
char buf[100];
cin >> buf;
ans[i] = 0;
for(int j = 0; j < m; ++j){
if(buf[j]-'0') ans[i] |= 1 << j;
}
}
memset(dp,-1,sizeof(dp));
cout << rec(0,0) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static int m, n;
static ArrayList<String> data;
static int[][] memo;
static int toInt(boolean[] bool) {
int ret = 0;
for (boolean b: bool) {
ret *= 2;
if (b) ret += 1;
}
return ret;
}
// used: trueの位置はすべて同じ値 isZero: usedのtrueの位置に対して、その値が0か1か
static int calc(boolean[] used, boolean[] isZero, ArrayList<String> rest) {
int key = toInt(used), key2 = toInt(isZero);
if (memo[key][key2] != -1) return memo[key][key2];
if (rest.size() == 0 || rest.size() == 1) return 0;
int ans = 12;
for (int i = 0; i < m; ++i) {
if (used[i]) continue;
ArrayList<String> zeros = new ArrayList<>();
ArrayList<String> ones = new ArrayList<>();
for (String str : rest) {
if (str.charAt(i) == '0') zeros.add(str);
else ones.add(str);
}
used[i] = true;
boolean[] tmp = new boolean[m];
System.arraycopy(isZero, 0, tmp,0, m);
tmp[i] = true;
boolean[] tmp2 = new boolean[m];
System.arraycopy(isZero, 0, tmp2,0, m);
tmp2[i] = false;
ans = Math.min(ans, 1 + Math.max(calc(used, tmp, zeros), calc(used, tmp2, ones)));
used[i] = false;
}
memo[key][key2] = ans;
return ans;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
m = scanner.nextInt();
n = scanner.nextInt();
if (m == 0 && n == 0) break;
data = new ArrayList<>();
memo = new int[(1 << m)][(1 << m)];
for (int i = 0; i < (1 << m); ++i) {
for (int j = 0; j < (1 << m); ++j) {
memo[i][j] = -1;
}
}
for (int i = 0; i < n; ++i) {
data.add(scanner.next());
}
System.out.println(calc(new boolean[m], new boolean[m], data));
}
}
}
| JAVA |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<functional>
#include<queue>
#include <iomanip>
#include<map>
#include<limits>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>
#define DBG cerr << '!' << endl;
#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(int (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(int (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int i = 0;i < (n);i++)cerr << v[i] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;
int mp[81*81*27 + 1];
int n,m;
int chenge(vector<int> v)
{
int ret = 0;
REP(i,v.size())
{
ret = ret * 3 + v[i];
}
return ret;
}
int dfs(vector<string> sv,vector<int> lv)
{
int now = chenge(lv);
if(mp[now] != -1)
{
return mp[now];
}
if(sv.size() <= 1)
{
return 0;
}
int ret = 100000;
REP(i,m)
{
bool flag = false;
REP(j,sv.size())
{
if(sv[0][i] != sv[j][i])flag = true;
}
if(!flag)continue;
vector<string> A,B;
REP(j,sv.size())
{
if(sv[j][i] == '1')
{
A.PB(sv[j]);
}
else
{
B.PB(sv[j]);
}
}
vector<int> X,Y;
X = lv;Y = lv;
X[i] = 1;Y[i] = 2;
ret = min(ret,max(dfs(A,X),dfs(B,Y))+1);
}
return mp[now] = ret;
}
int main()
{
while(cin >> m >> n,m|n)
{
memset(mp,-1,sizeof(mp));
vector<string> v;
REP(i,n)
{
string tmp;
cin >> tmp;
v.PB(tmp);
}
cout << dfs(v,vector<int>(m)) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int m, n;
int mem[177147];
int rec(const vector<bitset<11>>& v, int S = 0){
if(v.size() <= 1) return 0;
int& res = mem[S];
if(res != -1) return res;
res = m;
for(int i = 0;i < m;++i){
if(S/(int)pow(3, i)%3 != 0) continue;
vector<bitset<11>> a, b;
for(auto bit : v) if(bit[i]) a.push_back(bit); else b.push_back(bit);
res = min(res, 1+max(rec(a, S+(int)pow(3, i)), rec(b, S+2*(int)pow(3, i))));
}
return res;
}
int main(){
while(cin >> m >> n, m+n){
fill(begin(mem), end(mem), -1);
vector<bitset<11>> v(n);
for(auto& bit : v) cin >> bit;
cout << rec(v) << endl;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
using namespace std;
map<__uint128_t,int>memo;
int M,N;
vector<string>v;
int dfs(__uint128_t t){
if(memo.find(t)!=memo.end())return memo[t];
int c=0;
for(int i=0;i<N;i++){
if(t&(__uint128_t)(1)<<i)c++;
}
if(c==1)return 0;
int r=1<<30;
for(int j=0;j<M;j++){
__uint128_t a=0,b=0;
for(int i=0;i<N;i++){
__uint128_t e=(__uint128_t)(1)<<i;
if(t&e)(v[i][j]=='0'?a:b)|=e;
}
if(a&&b)r=min(r,max(dfs(a),dfs(b))+1);
}
return memo[t]=r;
}
int main(){
for(;cin>>M>>N,M;){
v.resize(N);
for(int i=0;i<N;i++)cin>>v[i];
__uint128_t t=0;
for(int i=0;i<N;i++)t|=(__uint128_t)(1)<<i;
memo.clear();
printf("%d\n",dfs(t));
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<math.h>
#include<climits>
using namespace std;
typedef unsigned long long ll;
const int mod=1000000007;
int m,n;
string g[129];
map<vector<string>,int>ma;
int dfs(vector<string>q,int de){
if(q.size()<2)return 0;
if(ma[q])return ma[q];
int ret=INT_MAX;
for(int i=0;i<m;i++){
vector<string>zero,one;
for(int j=0;j<q.size();j++){
if(q[j][i]=='0'){
zero.push_back(q[j]);
} else {
one.push_back(q[j]);
}
}
if(zero.size()==q.size()||one.size()==q.size()){
continue;
}
int o=dfs(one,de+1);
int z=dfs(zero,de+1);
if(max(o,z)+1<ret){
ret=max(o,z)+1;
}
//ret=min(ret,min(dfs(one),dfs(zero))+1);
}
return ma[q]=ret;
}
int main(){
while(cin>>m>>n,m){
vector<string>q;
for(int i=0;i<n;i++){
cin>>g[i];
q.push_back(g[i]);
}
cout<<dfs(q,0)<<endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
using namespace std;
int m,n;
int calc(vector<bool> &p,vector<string>&v,map<vector<bool>,int> &mp){
if(mp.find(p)!=mp.end())return mp[p];
int mi=10000;
for(int i=0;i<m;i++){
vector<bool>q(n),r(n);
for(int j=0;j<n;j++){
if(p[j]&&v[j][i]=='1'){
q[j] = 1;
}else if(p[j]&&v[j][i]=='0'){
r[j] = 1;
}
}
bool flag = 0;
for(int j=0;j<n;j++){
if(q[j])flag = 1;
}
bool fflag=0;
for(int j=0;j<n;j++){
if(r[j])fflag = 1;
}
if(flag&&fflag){
mi = min(mi,max(calc(q,v,mp),calc(r,v,mp))+1);
}
}
return mp[p] = mi;
}
int main(){
while(cin >> m >> n &&m!=0){
vector<string>v(n);
for(int i=0;i<n;i++){
cin >> v[i];
}
map<vector<bool>,int> mp;
vector<bool>p(n);
mp[p]= 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j)p[j] = 1;
else p[j] = 0;
}
mp[p]=0;
}
for(int i=0;i<n;i++){
p[i] = 1;
}
cout << calc(p,v,mp) << endl;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | // 2009ツアツジツアツ地ツ凝ヲツ予ツ選 H : Twenty Questions
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
using namespace std;
map<pair<vector<int>, int>, int> mp;
string qes[128];
int search(vector<int> &vi, int depth, int used, int m, int n){
if(mp.count(make_pair(vi, used))) return mp[make_pair(vi,used)];
if(vi.size() > (1<<(m-depth))) return 100;
if(vi.size() == 1) return depth;
int res = 100;
for(int i=0;i<m;i++){
if((used>>i)&1) continue;
vector<int> a, b;
for(int j=0;j<vi.size();j++){
if(qes[vi[j]][i]=='0') a.push_back(vi[j]);
else b.push_back(vi[j]);
}
if(a.empty()||b.empty()) continue;
res = min(res, max(search(a, depth+1, used|(1<<i), m, n), search(b, depth+1, used|(1<<i), m, n)));
}
return mp[make_pair(vi,used)] = res;
}
int main(){
int m, n;
while(cin >> m >> n, m){
for(int i=0;i<n;i++) cin >> qes[i];
vector<int> vi(n);
for(int i=0;i<n;i++) vi[i] = i;
mp.clear();
cout << search(vi, 0, 0, m, n) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,m,A[128];
map<set<int>,int>M;
int dfs(set<int> st){
if(st.size()<=1)return 0;
if(M.count(st))return M[st];
int res=99;
r(i,m){
set<int>S1,S2;
for(set<int>::iterator it=st.begin();it!=st.end();it++){
int x=*it;
if((1<<i)&A[x])S1.insert(x);
else S2.insert(x);
}
if(S1.size()==st.size())continue;
if(S2.size()==st.size())continue;
res=min(res,max(dfs(S1),dfs(S2))+1);
}
return M[st]=res;
}
int main(){
while(cin>>m>>n,n){
M.clear();
r(i,n){
string s;
cin>>s;
A[i]=0;
r(j,m)if(s[j]=='1')A[i]|=(1<<j);
}
set<int>st;
r(i,n)st.insert(i);
cout<<dfs(st)<<endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
const ld eps = 1e-6;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
vector<string> v;
int n, m;
int cnum(int &d, int &s) {
int cnt = 0;
rep(i, n) {
bool f = true;
rep(j, m) {
if (s&(1 << j)) {
if ((v[i][j] == '1') != ((d&(1 << j))==0))f = false;
}
}
if (f)cnt++;
}
return cnt;
}
map<P, bool> used;
map<P, int> res;
int dfs(int d,int s) {
int z = cnum(d, s);
if (z==1)return res[{d, s}]=0;
else if (z == 0)return res[{d, s}] = mod;
if (used[{d, s}])return res[{d, s}];
used[{d, s}] = true;
int ret = mod;
rep(i, m) {
if (s & (1 << i))continue;
int ns = s ^ (1 << i);
int nd = d ^ (1 << i);
int le = dfs(d, ns), ri = dfs(nd, ns);
ret = min(ret, max(le, ri) + 1);
}
return res[{d, s}] = ret;
}
void solve() {
used.clear(); res.clear();
v.clear(); v.resize(n);
rep(i, n) {
cin >> v[i];
}
int ans = dfs(0, 0);
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(9);
while (cin >>m>>n,n) {
solve();
}
//stop
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int solve(vector<int> &v, int m, int asked, int sig,
vector<vector<int> > &memo)
{
int n = v.size();
int res = 128;
int count = 0;
for (int j = 0; j < n; j++) {
if ((v[j] & asked) == (sig & asked)) {
count++;
}
}
if (count == 0) {
return -1;
}
if (count == 1) {
return 0;
}
if (memo[asked][sig] != 0) {
return memo[asked][sig];
}
for (int j = 0; j < m; j++) {
if ((asked & (1 << j)) == 0) {
int l = solve(v, m, asked | (1 << j), sig, memo);
int r = solve(v, m, asked | (1 << j), sig | (1 << j), memo);
res = min(res, max(l, r) + 1);
}
}
memo[asked][sig] = res;
return res;
}
int main()
{
int m, n;
scanf("%d%d", &m, &n);
while (m != 0) {
vector<int> v(n);
vector<vector<int> > memo(1 << m, vector<int>(1 << m));
for (int j = 0; j < n; j++) {
char s[12];
scanf("%s", &s);
for (int k = 0; k < m; k++) {
if (s[k] == '1') {
v[j] |= (1 << k);
}
}
}
int res = solve(v, m, 0, 0, memo);
printf("%d\n", res);
scanf("%d%d", &m, &n);
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
//#define int long long
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define all(x) x.begin(), x.end()
using pii = pair<int, int>;
using ll = long long;
bool candiv[202][202];
string ss[202];
int M, N;
int dat[150];
int dp[1 << 11][1 << 11];
int rec(int used, int yn) {
if (dp[used][yn] >= 0) return dp[used][yn];
int cnt = 0;
REP(i, N) {
if ((used & dat[i]) == yn) cnt++;
}
if (cnt <= 1) return dp[used][yn] = 0;
int ret = 1e9;
REP(j, M) {
if (used & (1 << j)) continue;
int nu = used | (1 << j);
int nyn1 = yn | (1 << j), nyn2 = yn;
int res = max(rec(nu, nyn1), rec(nu, nyn2)) + 1;
ret = min(ret, res);
}
return dp[used][yn] = ret;
}
bool solve() {
cin >> M >> N;
if (M == 0) return false;
REP(i, 1 << M) REP(j, 1 << M) dp[i][j] = -1;
REP(i, N) dat[i] = 0;
REP(i, N) {
string s;
cin >> s;
REP(j, M) {
if (s[j] == '1') dat[i] |= (1 << j);
}
}
int ans = rec(0, 0);
cout << ans << endl;
return true;
}
signed main() {
while (solve())
;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
int n,m;
string obj[222];
int memo[200200];
int rec(int state){
if(memo[state]>=0)return memo[state];
int cnt = 0, tmp = state;
vector<int> col(m);
rep(i,m){ col[i] = tmp%3; tmp/=3; }
rep(i,n){
bool f = true;
rep(j,m){
if(col[j] == 1 && obj[i][j] == '1')f = false;
if(col[j] == 2 && obj[i][j] == '0')f = false;
if(!f)break;
}
if(f)cnt++;
}
if(cnt<=1)return memo[state] = 0;
int res = m;
rep(i,m){
if(col[i]==0){
int maxv = 0;
for(int k=1;k<=2;k++){
col[i] = k;
int nstate = 0;
for(int j=m-1;j>=0;j--)nstate = nstate*3 + col[j];
maxv = max(maxv, rec(nstate));
}
res = min(res, maxv);
col[i] = 0;
}
}
return memo[state] = res+1;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
while(cin >> m >> n, m){
rep(i,n)cin >> obj[i];
memset(memo,-1,sizeof(memo));
cout << rec(0) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxm = 11, maxn = 130;
int n, m,f[1 << maxm][1 << maxm], mask[maxn], s[1 << maxm];
int main()
{
while (scanf("%d%d", &m, &n), m || n)
{
for (int i = 0; i < n; i++)
{
char str[12];
scanf("%s", str);
mask[i] = 0;
int len = strlen(str);
for (int j = 0; j < len; j++)
mask[i] |= (str[j] == '1') * (1 << j);
}
memset(f, 0, sizeof f);
for (int opt = (1 << m) - 2; opt >= 0; opt--)
{
memset(s, 0, sizeof s);
for (int j = 0; j < n; j++)
s[mask[j] & opt]++;
for (int opt0 = 0; opt0 <= opt; opt0++)
if (s[opt0] > 1)
{
f[opt][opt0] = n + 1;
for (int k = 0; k < m; k++)
if (!((1 << k) & opt))
f[opt][opt0] = min(f[opt][opt0], max(f[opt ^ (1 << k)][opt0], f[opt ^ (1 << k)][opt0 ^ (1 << k)]) + 1);
}
}
printf("%d\n", f[0][0]);
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <set>
#include <map>
using namespace std;
int M,N;
map<pair<set<int>,set<int>>,int> m;
char F[129][12];
int dfs(set<int> ob,set<int> fe){
if(ob.size()<=1) return (M-(int) fe.size());
if(m.count({ob,fe})) return m[{ob,fe}];
int res = 1e9;
set<int> fe2 = fe;
for(auto x:fe){
set<int> ob0,ob1;
for(auto y:ob){
if(F[y][x]=='0') ob0.insert(y);
else ob1.insert(y);
}
fe2.erase(x);
res = min(res,max(dfs(ob0,fe2),dfs(ob1,fe2)));
fe2.insert(x);
}
return m[{ob,fe}] = res;
}
int main(){
while(cin >> M >> N && N>0){
m.clear();
for(int i=0;i<N;i++) for(int j=0;j<M;j++) cin >> F[i][j];
set<int> ob,fe;
for(int i=0;i<N;i++) ob.insert(i);
for(int j=0;j<M;j++) fe.insert(j);
cout << dfs(ob,fe) << endl;
}
}
/*
#if
全探索するとO(2^n)かかるように見えるが、featureの状態を考えると、{質問されてyes,質問されてno,質問されていない}
の3状態しかない。つまり、ありえる状態はO(3^m)しかなく、メモ化再帰を行うことで十分間に合う。
setで状態を管理したが計算時間的に重くなってしまった気がする。
#endif
*/
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#include <cstdio>
using namespace std;
int str2bin(string s) {
int res = 0;
for(size_t i=0; i<s.size(); i++) {
res = res * 2 + (s[i] == '1');
}
return res;
}
const int INF = 1 << 28;
int M, N;
int dp[(1 << 11) + 1][(1 << 11) + 1];
int solve(vector<int> &vec, int query, int yn) {
// fprintf(stderr, "query = %d, yn = %d\n", query, yn);
if(dp[query][yn] < INF) return dp[query][yn];
int cnt = 0;
for(int i=0; i<N; i++) {
cnt += ((yn & query) == (vec[i] & query));
}
if(cnt < 2) return dp[query][yn] = 0;
else {
for(int i=0; i<M; i++) {
if(query >> i & 1) continue;
int nquery = query | (1 << i);
int val = max(solve(vec, nquery, yn|(1<<i)), solve(vec, nquery, yn)) + 1;
dp[query][yn] = min(dp[query][yn], val);
}
return dp[query][yn];
}
}
int main() {
while(cin >> M >> N, M || N) {
fill(dp[0], dp[1 << M], INF);
vector<int> vec(N);
for(int i=0; i<N; i++) {
string s; cin >> s;
vec[i] = str2bin(s);
}
cout << solve(vec, 0, 0) << endl;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <climits>
#include <bitset>
#include <cassert>
#include <random>
#include <complex>
#ifdef _MSC_VER
#include <agents.h>
#endif
#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int dp[1 << 22];
int cnt[1 << 22];
int m, n;
int rec(int mask, int bits){
int x = (mask << 11) | bits;
if (cnt[x] == 1) return 0;
if (cnt[x] == 0) return -1;
int &res = dp[x];
if (res + 1) return res;
res = 1e9;
rep(i, m){
if (mask >> i & 1) continue;
res = min(res, 1 + max(rec(mask | (1 << i), bits), rec(mask | (1 << i), bits | (1 << i))));
}
return res;
}
int item[200];
void setb(int &val, int i, int x){
if (x) val |= 1 << i;
else val &= ~(1 << i);
}
void dfs(int i, int mask){
if (i == m){
rep(j, n) ++cnt[(mask << 11) | (item[j] & mask)];
return;
}
rep(j, 2){
setb(mask, i, j);
dfs(i + 1, mask);
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
while (cin >> m >> n, n){
MEMSET(dp, -1);
MEMSET(cnt, 0);
rep(i, n){
string s;
cin >> s;
item[i] = 0;
rep(j, s.size()) if(s[j] - '0') item[i] |= 1 << j;
}
dfs(0, 0);
cout << rec(0, 0) << endl;
}
return 0;
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include "bits/stdc++.h"
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> E;
typedef long long LL;
string code[128];
int obj[128];
int m, n;
int memo[2048][2048];
int solve(int q, int a) {
if (memo[q][a] != -1) return memo[q][a];
int cnt = 0;
for (int i = 0; i < n; i++) {
if ((obj[i] & q) == a)cnt++;
}
if (cnt < 2) return memo[q][a] = 0;
int ret = m;
for (int i = 0; i < m; i++) {
int nq = q | (1 << i);
if (nq == q) continue;
ret = min(ret, max(solve(nq, a | (1 << i)), solve(nq, a)) + 1);
}
return memo[q][a] = ret;
}
int main() {
while (cin >> m >> n, m) {
for (int i = 0; i < n; i++) cin >> code[i];
memset(obj, 0, sizeof(obj));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (code[i][j] == '1') obj[i] |= (1 << j);
}
}
memset(memo, -1, sizeof(memo));
cout << solve(0, 0) << endl;
}
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <map>
#include <set>
using namespace std;
int n, m;
int Q[128];
int dp[1 << 11][1 << 11];
int dfs(int S, int T) {
if(dp[S][T] != -1) return dp[S][T];
int cnt = 0;
for(int i = 0; i < n; i++) {
if((Q[i] & S) == T) cnt++;
}
if(cnt <= 1) return 0;
int ret = m;
for(int i = 0; i < m; i++) {
if(S >> i & 1) continue;
int a = 0;
a = max(a, 1 + dfs(S | (1 << i), T));
a = max(a, 1 + dfs(S | (1 << i), T | (1 << i)));
ret = min(ret, a);
}
return dp[S][T] = ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
while(cin >> m >> n, n | m) {
for(int i = 0; i < n; i++) {
Q[i] = 0;
for(int j = 0; j < m; j++) {
char c;
cin >> c;
if(c == '1') Q[i] |= 1 << j;
}
}
memset(dp, -1, sizeof dp);
cout << dfs(0, 0) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cassert>
#include <stack>
#include <bitset>
using namespace std;
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<29;
typedef bitset<128> Bit;
int n, m;
string input[128];
map<vector<int>, int> memo;
// qS[i] = j : iツ氾板姪堋づ個篠ソツ姪「ツづーyes/noツづ猟伉用
int dfs(Bit manS, vector<int> qS) {
if (memo.count(qS)) return memo[qS];
if (manS.count() <= 1) {
return memo[qS] = 0;
}
int res = INF;
REP(i, m) {
if (qS[i]) continue;
Bit nextS1, nextS2;
REP(j, n) {
if (manS.test(j)) {
if (input[j][i] == '1') nextS1.set(j);
else nextS2.set(j);
}
}
vector<int> nextqS1 = qS, nextqS2 = qS;
nextqS1[i] = 1;
nextqS2[i] = 2;
res = min(res, max(dfs(nextS1, nextqS1), dfs(nextS2, nextqS2)) + 1);
}
// cout << manS << " ";
// REP(i, m) cout << qS[i];
// cout << " " << res << endl;
return memo[qS] = res;
}
int main() {
while(cin>>m>>n,n||m) {
REP(i, n) cin >> input[i];
Bit tmp;
REP(i, n) tmp.set(i);
memo.clear();
cout << dfs(tmp, vector<int>(m)) << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Scanner;
import java.util.function.BiFunction;
public class Main{
static Scanner scn = new Scanner(System.in);
static FastScanner sc = new FastScanner();
static PrintWriter ot = new PrintWriter(System.out);
static Random rand = new Random();
static int mod = 1000000007;
static long modmod = (long)mod * mod;
static long inf = (long)1e17;
static int[] dx = {0,1,0,-1};
static int[] dy = {1,0,-1,0};
static int[] dx8 = {-1,-1,-1,0,0,1,1,1};
static int[] dy8 = {-1,0,1,-1,1,-1,0,1};
static char[] dc = {'R','D','L','U'};
static BiFunction<Integer,Integer,Integer> fmax = (a,b)-> {return Math.max(a,b);};
static BiFunction<Integer,Integer,Integer> fmin = (a,b)-> {return Math.min(a,b);};
static BiFunction<Integer,Integer,Integer> fsum = (a,b)-> {return a+b;};
static BiFunction<Long,Long,Long> fmaxl = (a,b)-> {return Math.max(a,b);};
static BiFunction<Long,Long,Long> fminl = (a,b)-> {return Math.min(a,b);};
static BiFunction<Long,Long,Long> fsuml = (a,b)-> {return a+b;};
static BiFunction<Integer,Integer,Integer> fadd = fsum;
static BiFunction<Integer,Integer,Integer> fupd = (a,b)-> {return b;};
static BiFunction<Long,Long,Long> faddl = fsuml;
static BiFunction<Long,Long,Long> fupdl = (a,b)-> {return b;};
static String sp = " ";
static int M;
static long cnt;
static HashSet<Integer> q;
static HashMap<HashSet<Integer>,Integer> dp;
static String[] S;
public static void main(String[] args) {
//AOJ1302 Twenty
//author:Suunn
//状況ごとにうまく特定できるまで各質問を試してみる感じの全探索っぽい?
//再帰的にやると書きやすそう
//Mの指数でも間に合いそうだけど、計算量はよくわかりません。上からO(N M!)くらいかな...
//間に合わない これ可能か?
//再帰呼び出し回数を調べると6800万回。賢い枝狩りとかで行ける気もするけど...
//とりあえずまだ質問していない番号の集合を持ってみます
//けっこう早くなった
//そもそもこの方針じゃ無理っぽい?
//メモ化を試みる
while(true) {
M = sc.nextInt();
int N = sc.nextInt();
if(N==0)return;
HashSet<Integer> s = new HashSet<Integer>();
q = new HashSet<Integer>();
S = new String[N];
for(int i=0;i<N;i++)S[i] = sc.next();
for(int i=0;i<M;i++)q.add(i);
for(int i=0;i<N;i++)s.add(i);
dp = new HashMap<HashSet<Integer>,Integer>();
System.out.println(solve(s));
}
}
private static int solve(HashSet<Integer> s) {
if(dp.containsKey(s))return dp.get(s);
if(s.size()<=1) return 0;
cnt++;
int min = M+1;
HashSet<Integer> f = new HashSet<Integer>();
for(int e:q)f.add(e);
for(int e:f) {
HashSet<Integer> s0 = new HashSet<Integer>();
HashSet<Integer> s1 = new HashSet<Integer>();
for(int i:s) {
if(S[i].charAt(e)=='0')s0.add(i);
if(S[i].charAt(e)=='1')s1.add(i);
}
if(s0.size()==0||s1.size()==0)continue;//無駄な質問なのでしなくていい。これをcontinueしないと無限ループに陥る
q.remove(e);
min = Math.min(min,Math.max(solve(s0),solve(s1)));
q.add(e);
if(min==0)break;
}
dp.put(s,min+1);
return min+1;
}
}
class FastScanner {
private final java.io.InputStream in = System.in;
private final byte[] b = new byte[1024];
private int p = 0;
private int bl = 0;
private boolean hNB() {
if (p<bl) {
return true;
}else{
p = 0;
try {
bl = in.read(b);
} catch (IOException e) {
e.printStackTrace();
}
if (bl<=0) {
return false;
}
}
return true;
}
private int rB() { if (hNB()) return b[p++]; else return -1;}
private static boolean iPC(int c) { return 33 <= c && c <= 126;}
private void sU() { while(hNB() && !iPC(b[p])) p++;}
public boolean hN() { sU(); return hNB();}
public String next() {
if (!hN()) throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = rB();
while(iPC(b)) {
sb.appendCodePoint(b);
b = rB();
}
return sb.toString();
}
public char nextChar() {
return next().charAt(0);
}
public long nextLong() {
if (!hN()) throw new NoSuchElementException();
long n = 0;
boolean m = false;
int b = rB();
if (b=='-') {
m=true;
b=rB();
}
if (b<'0'||'9'<b) {
throw new NumberFormatException();
}
while(true){
if ('0'<=b&&b<='9') {
n *= 10;
n += b - '0';
}else if(b == -1||!iPC(b)){
return (m?-n:n);
}else{
throw new NumberFormatException();
}
b = rB();
}
}
public int nextInt() {
if (!hN()) throw new NoSuchElementException();
long n = 0;
boolean m = false;
int b = rB();
if (b == '-') {
m = true;
b = rB();
}
if (b<'0'||'9'<b) {
throw new NumberFormatException();
}
while(true){
if ('0'<=b&&b<='9') {
n *= 10;
n += b-'0';
}else if(b==-1||!iPC(b)){
return (int) (m?-n:n);
}else{
throw new NumberFormatException();
}
b = rB();
}
}
public int[] nextInts(int n) {
int[] a = new int[n];
for(int i=0;i<n;i++) {
a[i] = nextInt();
}
return a;
}
public int[] nextInts(int n,int s) {
int[] a = new int[n+s];
for(int i=s;i<n+s;i++) {
a[i] = nextInt();
}
return a;
}
public long[] nextLongs(int n, int s) {
long[] a = new long[n+s];
for(int i=s;i<n+s;i++) {
a[i] = nextLong();
}
return a;
}
public long[] nextLongs(int n) {
long[] a = new long[n];
for(int i=0;i<n;i++) {
a[i] = nextLong();
}
return a;
}
public int[][] nextIntses(int n,int m){
int[][] a = new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
a[i][j] = nextInt();
}
}
return a;
}
public String[] nexts(int n) {
String[] a = new String[n];
for(int i=0;i<n;i++) {
a[i] = next();
}
return a;
}
void nextIntses(int n,int[] ...m) {
int l = m[0].length;
for(int i=0;i<l;i++) {
for(int j=0;j<m.length;j++) {
m[j][i] = nextInt();
}
}
}
void nextLongses(int n,long[] ...m) {
int l = m[0].length;
for(int i=0;i<l;i++) {
for(int j=0;j<m.length;j++) {
m[j][i] = nextLong();
}
}
}
}
| JAVA |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<bits/stdc++.h>
using namespace std;
int m,n;
string s[130];
map<vector<int>,int> dp;
int rec(vector<int> S){
if(S.size()<=1)return 0;
if(dp[S]>0)return dp[S];
dp[S]=m;
for(int i=0;i<m;i++){
vector<int> ok,ng;
for(int j=0;j<S.size();j++){
if(s[S[j]][i]=='1')ok.push_back(S[j]);
else ng.push_back(S[j]);
}
if(ok.size()==0||ng.size()==0)continue;
dp[S]=min(dp[S],max(rec(ok),rec(ng))+1);
}
return dp[S];
}
int main(){
while(cin>>m>>n,n){
dp.clear();
for(int i=0;i<n;i++)cin>>s[i];
vector<int> S(n);
for(int i=0;i<n;i++)S[i]=i;
cout<<rec(S)<<endl;
}
return 0;
}
| CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
int m, n;
int ans[150];
int dp[2050][2050]; // S?????????????????£??????
using Set = bitset<12>;
int rec(int asked, int answer){ // ??????????????????aked???answer??¨??????????????????
int &res = dp[asked][answer];
if(res != -1) return res;
res = m;
// vector<int> a;
{
int cnt = 0;
for(int i = 0; i < n; ++i){
if((ans[i] & asked) == answer){
++cnt;
}
}
if(cnt <= 1) return res = 0;
}
// cout << "A " << Set(asked) << ' ' << Set(answer) << endl;
// cout << "B ";
// for(int &x : a) cout << Set(ans[x]) << ' ';
// cout << endl;
for(int i = 0; i < m; ++i){
if(asked >> i & 1) continue;
int cnt[2] = {};
for(int j = 0; j < n; ++j){
if((ans[j] & asked) == answer){
++cnt[ans[i] >> j & 1];
}
}
// if(cnt[0] == 0 || cnt[1] == 0) continue;
int tmp = 0;
for(int j = 0; j < 2; ++j){
tmp = max(tmp, 1 + rec(asked | 1<<i, answer | (j << i)));
}
res = min(res, tmp);
}
// assert(res != 1e9);
return res;
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
while(cin >> m >> n && m){
for(int i = 0; i < n; ++i){
char buf[100];
cin >> buf;
ans[i] = 0;
for(int j = 0; j < m; ++j){
if(buf[j]-'0') ans[i] |= 1 << j;
}
}
memset(dp,-1,sizeof(dp));
cout << rec(0,0) << endl;
// cout << "=================" << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | from collections import Counter
while True:
m, n = (int(s) for s in input().split())
if not m:
break
objs = [int(input(), 2) for i in range(n)]
dp = [bytearray(1 << m) for i in range(1 << m)]
bits = [1 << i for i in range(m)]
for mask in reversed(range((1 << m) - 1)):
s = Counter(obj & mask for obj in objs)
for masked, count in s.items():
if count > 1:
dp[mask][masked] = min(max(dp[mask + b][masked],
dp[mask + b][masked + b]) + 1
for b in bits if not b & mask)
print(dp[0][0]) | PYTHON3 |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include<iostream>
#include<vector>
#include<cstring>
using namespace std;
#define REP(i, N) for(int i = 0; i < (int)N; ++i)
// 3 ** 11
int N, M;
int num[200000];
int dp[200000];
int encode(string s) {
int h = 0;
for(int i = 0; i < s.size(); i++) {
h = h * 3 + s[i] - '0';
}
return h;
}
void decode(int c, string &s) {
REP(i, M) {
s[M - 1 - i] = '0' + c % 3;
c /= 3;
}
}
int get_num(string str) {
int state = encode(str);
if( num[state] >= 0 ) return num[state];
int res = 0;
for(int i = 0; i < str.size(); i++) {
if( str[i] == '2' ) {
str[i] = '0';
res += get_num(str);
str[i] = '1';
res += get_num(str);
break;
}
}
return num[state] = res;
}
int rec(string s) {
int state = encode(s);
if( num[state] <= 1 ) return 0;
if( dp[state] >= 0 ) return dp[state];
int res = 999999;
for(int i = 0; i < s.size(); i++) {
if( s[i] == '2' ) {
int t = 0;
s[i] = '0';
t = max(t, 1 + rec(s));
s[i] = '1';
t = max(t, 1 + rec(s));
s[i] = '2';
res = min(t, res);
}
}
return dp[state] = res;
}
int main() {
for(;;) {
cin >> M >> N;
if( N == 0 && M == 0 ) break;
memset(num, -1, sizeof(num));
memset(dp, -1, sizeof(dp));
REP(i, N) {
string str;
cin >> str;
int key = encode(str);
num[key] = 1;
}
int three = 1;
REP(i, M) three *= 3;
string s(M, '_');
REP(state, three) {
decode(state, s);
get_num(s);
}
REP(i, M) s[i] = '2';
int ans = rec(s);
cout << ans << endl;
}
} | CPP |
p00881 Twenty Questions | Consider a closed world and a set of features that are defined for all the objects in the world. Each feature can be answered with "yes" or "no". Using those features, we can identify any object from the rest of the objects in the world. In other words, each object can be represented as a fixed-length sequence of booleans. Any object is different from other objects by at least one feature.
You would like to identify an object from others. For this purpose, you can ask a series of questions to someone who knows what the object is. Every question you can ask is about one of the features. He/she immediately answers each question with "yes" or "no" correctly. You can choose the next question after you get the answer to the previous question.
You kindly pay the answerer 100 yen as a tip for each question. Because you don' t have surplus money, it is necessary to minimize the number of questions in the worst case. You don’t know what is the correct answer, but fortunately know all the objects in the world. Therefore, you can plan an optimal strategy before you start questioning.
The problem you have to solve is: given a set of boolean-encoded objects, minimize the maximum number of questions by which every object in the set is identifiable.
Input
The input is a sequence of multiple datasets. Each dataset begins with a line which consists of two integers, m and n: the number of features, and the number of objects, respectively. You can assume 0 < m ≤ 11 and 0 < n ≤ 128. It is followed by n lines, each of which corresponds to an object. Each line includes a binary string of length m which represent the value ("yes" or "no") of features. There are no two identical objects.
The end of the input is indicated by a line containing two zeros. There are at most 100 datasets.
Output
For each dataset, minimize the maximum number of questions by which every object is identifiable and output the result.
Example
Input
8 1
11010101
11 4
00111001100
01001101011
01010000011
01100110001
11 16
01000101111
01011000000
01011111001
01101101001
01110010111
01110100111
10000001010
10010001000
10010110100
10100010100
10101010110
10110100010
11001010011
11011001001
11111000111
11111011101
11 12
10000000000
01000000000
00100000000
00010000000
00001000000
00000100000
00000010000
00000001000
00000000100
00000000010
00000000001
00000000000
9 32
001000000
000100000
000010000
000001000
000000100
000000010
000000001
000000000
011000000
010100000
010010000
010001000
010000100
010000010
010000001
010000000
101000000
100100000
100010000
100001000
100000100
100000010
100000001
100000000
111000000
110100000
110010000
110001000
110000100
110000010
110000001
110000000
0 0
Output
0
2
4
11
9 | 7 | 0 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
const int P = 12;
int pw[P];
int m,n;
vector<int> x;
int in(){
string s;
cin >>s;
int ret = 0;
rep(i,m)if(s[i]=='1') ret |= (1<<i);
return ret;
}
int check(const vector<int> &s){
int ct = 0;
int must = 0;
rep(i,m)if(s[i]==1) must ^= (1<<i);
int cover = (1<<m)-1;
rep(i,m)if(s[i]==2) cover ^= (1<<i);
rep(i,n){
int t = x[i];
t ^= must;
t &= cover;
ct += !t;
}
return ct;
}
int make_mask(const vector<int> &s){
int ret = 0;
for(int i=m-1; i>=0; --i){
ret *= 3;
ret += s[i];
}
return ret;
}
int dp[540000];
int dfs(int mask){
if(dp[mask]>=0) return dp[mask];
vector<int> s(m);
int tmp = mask;
rep(i,m){
s[i] = tmp%3;
tmp /= 3;
}
if(check(s)<=1){
int ct = 0;
rep(i,m) ct += (s[i]!=2);
return dp[mask] = ct;
}
int ret = 101010;
// 次にどこを聞くか
rep(i,m)if(s[i]==2){
int zero,one;
s[i] = 0;
zero = make_mask(s);
s[i] = 1;
one = make_mask(s);
s[i] = 2;
ret = min(ret, max(dfs(zero),dfs(one)));
}
return dp[mask] = ret;
}
int main(){
pw[0] = 1;
for(int i=1; i<P; ++i) pw[i] = pw[i-1]*3;
while(cin >>m >>n,m){
x = vector<int>(n);
rep(i,n) x[i] = in();
memset(dp,-1,sizeof(dp));
cout << dfs(pw[m]-1) << endl;
}
return 0;
}
| CPP |
Subsets and Splits