我还是用了很朴素的暴力匹配A了这题,不得不感叹USACO时间放的好宽...

/*
ID: wushuai2
PROG: hamming
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int M = ;
const ll P = 10000000097ll ;
const int INF = 0x3f3f3f3f ;
const int MAX_N = ;
const int MAXSIZE = ; int n, b, d;
int ans[]; bool func(int b){
int i, j, cnt;
int t1[], t2[];
memset(t2, , sizeof(t2));
while(b){
t2[++t2[]] = b % ;
b = (b - b % ) / ;
}
for(int k = ; k <= ans[]; ++k){
cnt = ;
int a = ans[k];
memset(t1, , sizeof(t1));
while(a){
t1[++t1[]] = a % ;
a = (a - a % ) / ;
}
for(i = ; i <= Max(t1[], t2[]); ++i){
if(t1[i] != t2[i]) ++cnt;
}
if(cnt < d) return false;
}
return true;
} int main() {
ofstream fout ("hamming.out");
ifstream fin ("hamming.in");
int i, j, k, t, n, s, c, w, q;
fin >> n >> b >> d;
++ans[];
ans[] = ;
int num = ;
while(ans[] <= n){
if(func(num)){
++ans[];
ans[ans[]] = num;
}
++num;
}
for(i = ; i < ans[]; ++i){
fout << ans[i];
if(i == ans[] - ){
break;
}
if(i % == ) fout << endl;
else fout << ' ';
}
fout << endl; fin.close();
fout.close();
return ;
}

不过看了官方题解觉得很不错,来分享一下

for (a = 0; a < maxval; a++)
for (b = 0; b < maxval; b++) {
dist[a][b] = 0;
for (c = 0; c < B; c++)
if (((1 << c) & a) != ((1 << c) & b))
dist[a][b]++;
}

通过以上这段代码可以找到所有1 << B 中所有数的关系,就是二进制下不同的位数

然后通过一个DFS 来找可行对

void findgroups(int cur, int start) {
int a, b, canuse;
char ch;
if (cur == N) {
for (a = 0; a < cur; a++) {
if (a % 10)
fprintf(out, " ");
fprintf(out, "%d", nums[a]);
if (a % 10 == 9 || a == cur-1)
fprintf(out, "\n");
}
exit(0);
}
for (a = start; a < maxval; a++) {
canuse = 1;
for (b = 0; b < cur; b++)
if (dist[nums[b]][a] < D) {
canuse = 0;
break;
}
if (canuse) {
nums[cur] = a;
findgroups(cur+1, a+1);
}
}
}

不难得出,核心代码很短也很好写

找到全部N个数之后输出一下就可以了

USACO Hamming Codes DFS 构造的更多相关文章

  1. USACO Hamming Codes

    题目大意:求n个两两hamming距离大于等于d的序列,每个元素是一个b bit的数 思路:仍然暴力大法好 /*{ ID:a4298442 PROB:hamming LANG:C++ } */ #in ...

  2. USACO 2.1 Hamming Codes

    Hamming CodesRob Kolstad Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of ...

  3. 洛谷P1461 海明码 Hamming Codes

    P1461 海明码 Hamming Codes 98通过 120提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 给出 N,B 和 ...

  4. USACO hamming

    考试周终于过去了一半,可以继续写USACO了. 先来看一下题目吧. Hamming CodesRob Kolstad Given N, B, and D: Find a set of N codewo ...

  5. 洛谷 P1461 海明码 Hamming Codes

    P1461 海明码 Hamming Codes 题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B &l ...

  6. codeforces 681D Gifts by the List dfs+构造

    题意:给你一个森林,表示其祖先关系(自己也是自己的祖先),每个人有一个礼物(要送给这个人的固定的一个祖先) 让你构造一个序列,使得的对于每个人,这个序列中第一个出现的他的祖先,是他要送礼物的的那个祖先 ...

  7. 【USACO 2.1】Hamming Codes

    /* TASK: hamming LANG: C++ URL:http://train.usaco.org/usacoprob2?a=5FomsUyB0cP&S=hamming SOLVE: ...

  8. USACO Section2.1 Hamming Codes 解题报告 【icedream61】

    hamming解题报告----------------------------------------------------------------------------------------- ...

  9. USACO 2.1 海明码 Hamming Codes (模拟+位运算+黑科技__builtin_popcount(n))

    题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位 ...

随机推荐

  1. MVC-03 控制器(3)

    Controller负责处理浏览器来的所有要求,并决定响应什么属性给浏览器,以及协调Model与View之间的数据传递.在ASP.NET MVC中有好几种传递数据给视图的方式,例如从ASP.NET M ...

  2. IM与工作信息流整合

    IM与工作信息流整合,希望减轻用户“信息”负担   从36氪此前的<“明道”现在推出个人免费版本,是怎样的逻辑?>一 文中,我们可以了解到,国内现在的协作产品设计思路主要有两种:一种是像t ...

  3. Qt将窗体变为顶层窗体(activateWindow(); 和 raise() )

    我们知道,在windows上通过鼠标双击某应用程序图标,该应用程序往往会以顶层窗口的形式呈现在我们面前,但是对于一个已经打开的非顶层窗口,我们怎么将其激活为顶层窗口呢? 要达到激活,这个必须要满足两个 ...

  4. android 构建数据库SQLite

    1.首先我们需要一个空白的eclipse android工程 2.然后修改AndroidManifest.xml 在<application></application>标签里 ...

  5. LeetCode——Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  6. Objective-c Category(类别)

    category是Objective-c里面最常用的功能之一. category可以为已经存在的类增加方法,而不需要增加一个子类. 类别接口的标准语法格式如下: #import "类名.h& ...

  7. leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal

    这道题是为数不多的感觉在读本科的时候见过的问题. 人工构造的过程是如何呢.兴许遍历最后一个节点一定是整棵树的根节点.从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右 ...

  8. 【转】Ubuntu下deb包的安装方法

    [转]Ubuntu下deb包的安装方法 deb是debian linus的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb dpkg 是Debian P ...

  9. Node.js学习笔记2(安装和配置Node.js)

            1.安装         windows下安装,在http://nodejs.org下载安装包进行安装即可.         linux下安装,使用yum或者下载源码进行编译.     ...

  10. [LeetCode]题解(python):013-Roman to Integer

    题目来源: https://leetcode.com/problems/roman-to-integer/ 题意分析: 这道题目和上一题目相反,是将罗马数字转化成阿拉伯数字. 题目思路: 只要知道罗马 ...