1 条题解

  • 0
    @ 2025-4-9 0:42:29
    #include <iostream>
    #include <vector>
    #include <climits>
    using namespace std;
    
    int main() {
    	int t;
    	cin >> t;
    	while (t--) {
    		int n, g;
    		cin >> n >> g;
    		vector<vector<int>> current_candidates;
    		vector<int> target_bits(g);
    		
    		// 读取目标微缩片
    		string target_str;
    		cin >> target_str;
    		for (int i = 0; i < g; ++i) {
    			target_bits[i] = target_str[i] - '0';
    		}
    		current_candidates.push_back(target_bits);
    		
    		// 读取其他微缩片
    		for (int i = 1; i < n; ++i) {
    			string s;
    			cin >> s;
    			vector<int> bits(g);
    			for (int j = 0; j < g; ++j) {
    				bits[j] = s[j] - '0';
    			}
    			current_candidates.push_back(bits);
    		}
    		
    		bool processed[15] = {false};
    		int steps = 0;
    		
    		while (current_candidates.size() > 1) {
    			int best_bit = -1;
    			int min_count = INT_MAX;
    			
    			// 遍历所有未处理的位,找到最小的count
    			for (int k = 0; k < g; ++k) {
    				if (processed[k]) continue;
    				
    				int count = 0;
    				for (const auto& m : current_candidates) {
    					if (m[k] == target_bits[k]) {
    						++count;
    					}
    				}
    				
    				// 选择最小的count,或者相同count时选最小的位
    				if (count < min_count || (count == min_count && k < best_bit)) {
    					min_count = count;
    					best_bit = k;
    				}
    			}
    			
    			if (best_bit == -1) break; // 所有位都已处理,但理论上不可能
    			
    			// 过滤候选集
    			vector<vector<int>> new_candidates;
    			for (const auto& m : current_candidates) {
    				if (m[best_bit] == target_bits[best_bit]) {
    					new_candidates.push_back(m);
    				}
    			}
    			current_candidates = move(new_candidates);
    			processed[best_bit] = true;
    			++steps;
    		}
    		
    		cout << steps << endl;
    	}
    	return 0;
    }
    • 1

    信息

    ID
    233
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    5
    已通过
    0
    上传者