博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
USACO hamming
阅读量:4684 次
发布时间:2019-06-09

本文共 2218 字,大约阅读时间需要 7 分钟。

考试周终于过去了一半,可以继续写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 0100Bit 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 7682 85 97 102 120 127

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

/**ID: njuwz151TASK: hammingLANG: C++*/#include 
#include
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

你可能感兴趣的文章
CF715C Digit Tree
查看>>
二分法练习1
查看>>
QT 制作串口调试小助手----(小白篇)
查看>>
前端MVC实践之hellorocket——by张舒彤
查看>>
OptimalSolution(2)--二叉树问题(3)Path路径问题
查看>>
IPC 之 Messenger 的使用
查看>>
macos 下usb键盘问题.
查看>>
SQL函数学习(十六):STUFF()函数
查看>>
Apache Hadoop 和Hadoop生态圈
查看>>
Ctrl+Enter 选中文本提交
查看>>
android WIFI
查看>>
常用的匹配正则表达式和实例
查看>>
小组成员及其git链接
查看>>
SQL case when else
查看>>
MVc Identity登陆锁定
查看>>
cdn连接失败是什么意思_关于CDN的原理、术语和应用场景那些事
查看>>
ultraedit26 运行的是试用模式_免费试用U盘数据恢复工具 – 轻松找回U盘丢失的各种数据!...
查看>>
python sum函数导入list_python sum函数iterable参数为二维list,start参数为“[]”该如何理解...
查看>>
UVa540 Team Queue
查看>>
android 练习之路 (八)
查看>>