考试周终于过去了一半,可以继续写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 01000x234 = 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 = 9;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[1 << maxn];result[0] = 0;int n_ptr = 1;for(int i = 1; i < (1 << b); i++) {if(n_ptr > n) {break;}bool can_add = true;for(int j = 0; 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 = 0; i < n - 1; i++) {cout << result[i];if((i + 1) % 10) {cout << " ";} else {cout << endl;}}cout << result[n - 1] << endl;
} int count(int a, int b) {int result = 0;for(int i = 0; i < maxn; i++) {if(((a>>i) & 1) != ((b>>i) & 1)) {result++;}}return result;
}

转载于:https://www.cnblogs.com/NJUWZ/p/7093126.html

USACO hamming相关推荐

  1. usaco Hamming Codes

    就是在二进制上的枚举,B位的意思就是十进制不超过1<<B,数字很小枚举即可每枚举一个跟前面的进行比较看符不符合题意,就是进行按位异或然后统计其中1的个数大于等于D就符合题意. /* ID: ...

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

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

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

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

  4. USACO 做题小结

    还记得之前,发过一篇阶段性总结与未来规划..结果由于最近rp爆发(保研成功+进wf)后者显然靠bin神,前者也是运气.因此,放松了一段时间.然后就开始刷usaco了,原因是不用花时间找解题报告在NOC ...

  5. 洛谷P1461 海明码 Hamming Codes

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

  6. usaco Shaping Regions

    这就是usaco 前面的windows area的变形. /* ID:jinbo wu TASK:rect1 LANG:C++ */ #include<iostream> #include ...

  7. usaco Postal Vans(dp)

    是哈密顿回路,然后...就不知道怎么写了 ,以前写过类似的不过情况没这么多也没这么复 usaco training 6.1.1 Postal Vans 题解 标签: usaco training题解d ...

  8. usaco Beef McNuggets

    这两天贼烦,ccf炸了,还有一个烦心事.哎我都不知道自己能不能坚持下去了.马上期末考了.这段时间还是抓紧时间复习吧同时刷usaco的节奏要跟以前一样了,毕竟课少了. 题解: 只要你知道以下的数论结论, ...

  9. usaco前两章小结

    usaco 暑假老师有推荐做但是那个题目太长了,而且·大部分都是废话做起来特别慢,而且当时自己基本上什么都不懂,太难了所以看了题解做了两题就放弃了. 转眼就上学了,因为想学习acm所以就胡乱找题做但是 ...

最新文章

  1. IDEA设置单击左侧项目文件,自动在右侧编辑器打开
  2. Spring dataSource配置
  3. uView(Tabbar 底部导航栏)
  4. python凹多边形分割_使用Opencv python从图像裁剪凹面多边形
  5. go tool vet是你的好朋友
  6. 推荐系统--MovieLens数据集
  7. 五线谱如何快速修改谱号,Overture这个小技巧必须要掌握!
  8. VC浏览器相关的学习(七)(BHO捕获鼠标键盘事件)
  9. 升级版飞机大战来袭,手把手教学,万字详解,你还学不会嘛?
  10. poj1639 Picnic Planning 最小度数限制生成树
  11. 初中数学分几个模块_初中数学有几部分
  12. 坦克世界服务器系统不更新失败怎么办,坦克世界安装更新数据失败怎么办? 爱问知识人...
  13. python爬虫爬取某网站全站图片案列(源码全给可白漂,仅供学习使用,切勿用作非法用途)
  14. 大文件上传+解决方案
  15. 分布式session解决——Spring-data-redis
  16. win7计算机开机启动项设置,启动项管理,教您电脑开机启动项怎么设置
  17. Jackson详细介绍
  18. docker容器挂载权限问题 导致日志文件不生成
  19. 编程猫计算机软件编程蓝桥杯,蓝桥杯大赛编程猫强势夺魁,参赛学员获奖率高达近90%...
  20. android物品管理软件,物品寄存管理系统下载

热门文章

  1. html 隐藏整个fieldset,HTML fieldset disabled 属性 | 菜鸟教程
  2. html中fieldset改颜色,HTML fieldset 标签的具体用法是什么?怎么用fieldset设置边框颜色?...
  3. Spring注解驱动开发第11讲——面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?
  4. Let's implement a Gaussian Naive Bayes classifier in Python
  5. eval()替代方法
  6. 软件项目测试的具体内容
  7. 富士施乐 C2560 C3060 出现代码:116-331 故障分析
  8. mac for intel 使用个人账号免费使用vmware
  9. 在cocos2d-x3.0里面如何使用物理引擎:弹球
  10. I2C软件模拟EEPROM通讯实验