USACO hamming
考试周终于过去了一半,可以继续写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的更多相关文章
- USACO Hamming Codes DFS 构造
我还是用了很朴素的暴力匹配A了这题,不得不感叹USACO时间放的好宽... /* ID: wushuai2 PROG: hamming LANG: C++ */ //#pragma comment(l ...
- USACO Hamming Codes
题目大意:求n个两两hamming距离大于等于d的序列,每个元素是一个b bit的数 思路:仍然暴力大法好 /*{ ID:a4298442 PROB:hamming LANG:C++ } */ #in ...
- 【USACO 2.1】Hamming Codes
/* TASK: hamming LANG: C++ URL:http://train.usaco.org/usacoprob2?a=5FomsUyB0cP&S=hamming SOLVE: ...
- USACO Section2.1 Hamming Codes 解题报告 【icedream61】
hamming解题报告----------------------------------------------------------------------------------------- ...
- USACO 2.1 海明码 Hamming Codes (模拟+位运算+黑科技__builtin_popcount(n))
题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位 ...
- USACO Section 2.1: Hamming Codes
挺简单的一道题 /* ID: yingzho1 LANG: C++ TASK: hamming */ #include <iostream> #include <fstream> ...
- USACO 2.1 Hamming Codes
Hamming CodesRob Kolstad Given N, B, and D: Find a set of N codewords (1 <= N <= 64), each of ...
- 洛谷P1461 海明码 Hamming Codes
P1461 海明码 Hamming Codes 98通过 120提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 给出 N,B 和 ...
- P1461 海明码 Hamming Codes
题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位 ...
随机推荐
- Tomcat--各个目录详解(二)
Tomcat整体目录: 一.bin文件(存放启动和关闭tomcat脚本) 其中.bat和.sh文件很多都是成对出现的,作用是一样的,一个是Windows的,一个是Linux. ① startup文件: ...
- python 爬取淘宝的模特照片
前段时间花了一部分时间学习下正则表达式,总觉得利用正则要做点什么事情,所以想通过爬取页面的方式把一些美女的照片保存下来,其实过程很简单. 1.首先读取页面信息: 2.过滤出来照片的url地址: 3.通 ...
- Natas Wargame Level 3 Writeup 与 robots.txt
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnsAAAC5CAYAAABQi/kBAAAABHNCSVQICAgIfAhkiAAAIABJREFUeF
- 微信小程序对医疗创业的启示,“餐饮+微信小程序”的猜想
一:微信小程序对医疗创业的启示:如何用完即走 仔细看了张小龙在28日微信公开课上发布小程序时的演讲全文,我觉得对解决当下医疗创业的困惑有着巨大的启发.没准还能开辟新的未来. 张小龙对小程序精髓的阐释是 ...
- Ajax02 json
1 什么是json JavaScript Object Notation(JavaScript 对象表示法) 是一种轻量级的数据交换格式. 注: 数据交换:将数据先转换成一种与平台无关的数据 格式(比 ...
- Deep Learning in R
Introduction Deep learning is a recent trend in machine learning that models highly non-linear repre ...
- C语言数组之冒泡排序+折半查找法(二分查找)
冒泡排序算法 将相邻的元素进行两两比较,大的向后"冒", 小的向前"赶". 口诀: N个数字来排队,两两比较小靠前 外层循环N-1(控制需要比较的轮数). 内层 ...
- ArrayList实现分组功能
这边只用2个属性来进行分组 定义Object有key与value属性 按照key的不同对于arrayList进行分组 List<Object> tempList=new ArrayList ...
- Python教程(2.5)——控制台输入
写Python程序时,你可能希望用户与程序有所交互.例如你可能希望用户输入一些信息,这样就可以让程序的扩展性提高. 这一节我们来谈一谈Python的控制台输入. 输入字符串 Python提供一个叫做i ...
- JSON数据解析:Gson(谷歌)和fastjson(阿里巴巴)的异同点
Gson和fastjson分别为谷歌和阿里巴巴对JSON数据进行处理封装的jar包 Gson(谷歌)和fastjson(阿里巴巴)两者异同点: 相同点:都是根据JSON数据创建相应的类 不同点: 1. ...