• USACO hamming 继续暴搜


    USER: Kevin Samuel [kevin_s1]
    TASK: hamming
    LANG: C++
    
    Compiling...
    Compile: OK
    
    Executing...
       Test 1: TEST OK [0.003 secs, 3504 KB]
       Test 2: TEST OK [0.005 secs, 3504 KB]
       Test 3: TEST OK [0.008 secs, 3504 KB]
       Test 4: TEST OK [0.008 secs, 3504 KB]
       Test 5: TEST OK [0.008 secs, 3504 KB]
       Test 6: TEST OK [0.005 secs, 3504 KB]
       Test 7: TEST OK [0.008 secs, 3504 KB]
       Test 8: TEST OK [0.005 secs, 3504 KB]
       Test 9: TEST OK [0.005 secs, 3504 KB]
       Test 10: TEST OK [0.008 secs, 3504 KB]
       Test 11: TEST OK [0.008 secs, 3504 KB]
    
    All tests OK.
    

    YOUR PROGRAM ('hamming') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated

    congratulations.

    /*
    ID:kevin_s1
    PROG:hamming
    LANG:C++
    */
    
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <set>
    #include <algorithm>
    #include <cstdlib>
    #include <list>
    #include <bitset>
    #include <cmath>
    
    using namespace std;
    
    //gobal variable====
    int N, B, D;
    vector<bitset<8> > result;//bitset能够加速
    string str = "11111111";
    bitset<8> last(str);
    int MaxDeep;
    //==================
    
    
    //function==========
    
    int hammingDis(bitset<8> a, bitset<8> b){
    	bitset<8> x = a xor b;
    	return x.count();
    }
    
    bool check(bitset<8> bit){//推断是否与前面的数距离小于D
    	for(int i = 0; i < result.size(); i++){
    		if(hammingDis(bit, result[i]) < D)
    			return false;
    	}
    	return true;
    }
    
    void DFS(int deep, int count){//deep为搜索的数字,count为确认合法数字的个数,从小往大搜
    	if(deep > MaxDeep)
    		return;
    	if(count > N)
    		return;
    	//
    	bitset<8> tmp(deep + 1);
    	//int dist = hammingDis(last, tmp);
    	if(/*dist >= D*/check(tmp) == true){
    		result.push_back(tmp);
    		last = tmp;
    		DFS(deep + 1, count + 1);
    	}
    	else{
    		DFS(deep + 1, count);
    	}
    	return;
    }
    //==================
    
    int main(){
    	freopen("hamming.in","r",stdin);
    	freopen("hamming.out","w",stdout);
    	cin>>N>>B>>D;
    	MaxDeep = (int)pow(2.0, (double)B) - 1;
    	DFS(-1, 0);
    	for(int i = 0; i < N; i++){
    		cout<<result[i].to_ulong();
    		if(i != N - 1){
    			if(i % 10 == 9)
    				cout<<endl;
    			else
    				cout<<" ";
    		}
    	}
    	cout<<endl;
    	return 0;
    }
    


  • 相关阅读:
    剑指Offer——数组中重复的数字
    基于Google Protobuff和Mina的RPC
    Google Protocol Buffers 编码(Encoding)
    Protocol Buffers 语法指南
    Google Protocol Buffers 入门
    Google Protocol Buffers 概述 转
    Protobuf语言指南
    基于Protobuf的通讯库--Poppy简介
    log4cxx第三篇----使用多个logger
    log4CXX第二篇---配置文件(properties文件)详解
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7215841.html
Copyright © 2020-2023  润新知