我还是用了很朴素的暴力匹配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. codeforces 557D. Vitaly and Cycle 二分图染色

    题目链接 n个点, m条边, 问最少加几条边可以出现一个奇环, 在这种情况下, 有多少种加边的方式. 具体看代码解释 #include<bits/stdc++.h> using names ...

  2. java集合分析(转载)

    参考文章:浅谈Java中的Set.List.Map的区别 Java 7 Collections详解 java中集合分为三类: Set(集) List(列表) Map(映射) Set和List继承自Co ...

  3. java 类排序

    参考文档:Java排序: Comparator vs Comparable 入门 java类经常面临排序问题,据我所知:java提供comparable和comparator两种比较方式: 1. co ...

  4. lambda, reduce, map求阶乘之和

    学完这几个优雅的内建函数,就可以做一些有趣的小练习来激发兴趣了.而python最大的好处便是简洁,看下边要求 用1行代码求 1! + 2! + 3! + ... + 10! 求阶乘 reduce函数用 ...

  5. 10min系列之二日志可视化进阶

    10min系列之二日志可视化进阶(作者原创,同步发布在github) 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 本文所有的demo,都是浏览器下展示的 原创 ...

  6. 修改html很实用的insertAdjacentHTML方法

    添加HTML内容与文本内容以前用的是innerHTML与innerText方法, 最近发现还有insertAdjacentHTML和 insertAdjacentText方法, 这两个方法更灵活,可以 ...

  7. Qt Creator needs a compiler set up to build. Configure a compiler in the kit options - Stack Overflow

    Qt Creator needs a compiler set up to build. Configure a compiler in the kit options - Stack Overflo ...

  8. 2014 android毕设代做 代做Android毕设 安卓毕设

    近期略有闲暇. 看到部分老友帮忙做毕业设计. 一来能够锻炼下小技能,二来能够得点零花小钱. 心遂小动.跃跃欲试. 毕设这件事情,个人觉得还是自己做的好. 只是每一个人都有各种各样的原因不能自己完毕. ...

  9. BFG

    "/"应用程序中的服务器错误. 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 提供程序集合中不 ...

  10. string.Format()字符串格式化

    Format()基本语法:     {索引[,对齐][:格式字符串]}     ·索引:表示引用的对象列表中的第n个对象参数.     ·对齐(可选):设置宽度与对齐方式,该参数为带符号的整数.正数为 ...