考试周终于过去了一半,可以继续写USACO了。

先来看一下题目吧。

Hamming Codes
Rob Kolstad

Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of length B bits (1 <= B <= 8), such that each of the codewords is at least Hamming distance of D (1 <= D <= 7) away from each of the other codewords. The Hamming distance between a pair of codewords is the number of binary bits that differ in their binary notation. Consider the two codewords 0x554 and 0x234 and their differences (0x554 means the hexadecimal number with hex digits 5, 5, and 4):

        0x554 = 0101 0101 0100
0x234 = 0010 0011 0100
Bit differences: xxx xx

Since five bits were different, the Hamming distance is 5.

PROGRAM NAME: hamming

INPUT FORMAT

N, B, D on a single line

SAMPLE INPUT (file hamming.in)

16 7 3

OUTPUT FORMAT

N codewords, sorted, in decimal, ten per line. In the case of multiple solutions, your program should output the solution which, if interpreted as a base 2^B integer, would have the least value.

SAMPLE OUTPUT (file hamming.out)

0 7 25 30 42 45 51 52 75 76
82 85 97 102 120 127

这条题目不难,由于数据量很小,因此使用的方法是直接遍历。

/**
ID: njuwz151
TASK: hamming
LANG: C++
*/
#include <iostream>
#include <cstdio> using namespace std; const int maxn = ; int count(int a, int b); int main() {
freopen("hamming.in", "r", stdin);
freopen("hamming.out", "w", stdout); int n, b, d;
cin >> n >> b >> d; int result[ << maxn];
result[] = ;
int n_ptr = ;
for(int i = ; i < ( << b); i++) {
if(n_ptr > n) {
break;
}
bool can_add = true;
for(int j = ; j < n_ptr; j++) {
if(count(i, result[j]) < d) {
can_add = false;
break;
}
}
if(can_add) {
result[n_ptr] = i;
n_ptr++;
}
}
for(int i = ; i < n - ; i++) {
cout << result[i];
if((i + ) % ) {
cout << " ";
} else {
cout << endl;
}
}
cout << result[n - ] << endl;
} int count(int a, int b) {
int result = ;
for(int i = ; i < maxn; i++) {
if(((a>>i) & ) != ((b>>i) & )) {
result++;
}
}
return result;
}

USACO hamming的更多相关文章

  1. USACO Hamming Codes DFS 构造

    我还是用了很朴素的暴力匹配A了这题,不得不感叹USACO时间放的好宽... /* ID: wushuai2 PROG: hamming LANG: C++ */ //#pragma comment(l ...

  2. USACO Hamming Codes

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

  3. 【USACO 2.1】Hamming Codes

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

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

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

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

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

  6. USACO Section 2.1: Hamming Codes

    挺简单的一道题 /* ID: yingzho1 LANG: C++ TASK: hamming */ #include <iostream> #include <fstream> ...

  7. USACO 2.1 Hamming Codes

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

  8. 洛谷P1461 海明码 Hamming Codes

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

  9. P1461 海明码 Hamming Codes

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

随机推荐

  1. 关于Dubbo一个接口多个实现的解决方案

    如题,其实这个问题在官方文档中已经说明了.我直接贴图就好了 更多学习请参考:minglisoft.cn/technology

  2. DDD理论学习系列(3)-- 限界上下文

    1. 引言 限界上下文可以拆分为两个词,限界和上下文. 限界:是指一个界限,具体的某一个范围. 上下文:个人理解就是语境. 比如我们常说的段子: "我想静静." 这个句子一般是想表 ...

  3. JavaScript性能优化 DOM编程

    最近在研读<高性能JavaScript>,在此做些简单记录.示例代码可在此处查看到. 一.DOM 1)DOM和JavaScript 文档对象模型(DOM)是一个独立于语言的,用于操作XML ...

  4. 写给Android App开发人员看的Android底层知识(3)

    (七)App启动流程第2篇 书接上文,App启动一共有七个阶段,上篇文章篇幅所限,我们只看了第一阶段,接下来讲剩余的六个阶段,仍然是拿斗鱼App举例子. 简单回顾一下第一阶段的流程,就是Launche ...

  5. java关键字transient与volatile小结

    本文转自:http://heaven-arch.iteye.com/blog/1160693 transient和volatile两个关键字一个用于对象序列化,一个用于线程同步,都是Java中比较高阶 ...

  6. .Net Core应用搭建的分布式邮件系统设计

    本篇分享的是由NetCore搭建的分布式邮件系统,主要采用NetCore的Api和控制台应用程序,由于此系统属于公司的所以这里只能分享设计图和一些单纯不设计业务的类或方法: 为什么要在公司中首例采用N ...

  7. SmartCoder每日站立会议05

    1.站立会议内容 经过四天的努力,我们的微信小程序有了很大的进展,首页的设计定了方案,API接地图正在试着把消息展示到其中,争取把地图信息做到直观形象. 站立会议照片: 2.任务展板 3.燃尽图

  8. cmd批处理延迟代码 结束进程

    choice /t 5 /d y /n >nul taskkill /im chrome.exe /f pause

  9. Unity游戏程序员面试题及解答

    典型的一些如手写排序算法.一些基本数学问题,在此就不列举了.以下整理出一些代表性的.有参考价值的题,真实面试题,附有本人的解答,欢迎讨论. 题1.指出下列哪些属于值类型? int System.Obj ...

  10. Redis 小白指南(一)- 简介、安装、GUI 和 C# 驱动介绍

    Redis 小白指南(一)- 简介.安装.GUI 和 C# 驱动介绍 目录 简介 安装 入门指令 GUI 工具 C# 驱动介绍 简介 ANSI C 编写,开源,基于内存,可持久化,一个键值对的数据库, ...