USACO 2.1 Hamming Codes
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 题目大意:就是给你NBD三个正整数,意思是在0到2^B-1中选择N个数字,而且两两之间“距离”大于D。对“距离”是这么定义的,二进制对应位不同符号的个数。
思路:说实话是没有什么思路的,有想搜索但是好虚,觉得数据不小(选或者不选,复杂度是2^(2^B-1)),不敢写,后来上网看了解析,居然就是搜索,还是最裸的那种。。。有点想不明白,不过也提醒我,撑死胆大的,饿死胆小的。代码如下
/*
ID:fffgrdc1
PROB:hamming
LANG:C++
*/
#include<cstdio>
#include<iostream>
#include<cstdlib>
using namespace std;
int n,m,d,cnt=;
int topnum=;
int ans[]={};
bool check(int x,int y)
{
x=x^y;int tot=;
while(x)
{
if(x&)tot++;
x>>=;
}
return tot>=d;
}
void dfs(int x)
{
if(cnt==n)
{
int num=;
for(int i=;i<=cnt;i++)
{
num++;
if(num==)
printf("%d",ans[i]);
else printf(" %d",ans[i]);
if(num==)
{
printf("\n");
num=;
}
}
if(num)printf("\n");
exit();
}
if(x>topnum)return;
int flag=;
for(int i=;i<=cnt;i++)
{
if(!check(ans[i],x))
{
flag=;
break;
}
}
if(flag)
{
ans[++cnt]=x;
dfs(x+);
cnt--;
}
dfs(x+); }
int main()
{
freopen("hamming.in","r",stdin);
freopen("hamming.out","w",stdout);
scanf("%d%d%d",&n,&m,&d);
while(m--)topnum<<=;
topnum--;
//printf("%d\n",topnum);
dfs();
return ;
}
USACO 2.1 Hamming Codes的更多相关文章
- USACO Section2.1 Hamming Codes 解题报告 【icedream61】
hamming解题报告----------------------------------------------------------------------------------------- ...
- 洛谷P1461 海明码 Hamming Codes
P1461 海明码 Hamming Codes 98通过 120提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 给出 N,B 和 ...
- 洛谷 P1461 海明码 Hamming Codes
P1461 海明码 Hamming Codes 题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B &l ...
- 【USACO 2.1】Hamming Codes
/* TASK: hamming LANG: C++ URL:http://train.usaco.org/usacoprob2?a=5FomsUyB0cP&S=hamming SOLVE: ...
- USACO Hamming Codes DFS 构造
我还是用了很朴素的暴力匹配A了这题,不得不感叹USACO时间放的好宽... /* ID: wushuai2 PROG: hamming LANG: C++ */ //#pragma comment(l ...
- 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 Hamming Codes
题目大意:求n个两两hamming距离大于等于d的序列,每个元素是一个b bit的数 思路:仍然暴力大法好 /*{ ID:a4298442 PROB:hamming LANG:C++ } */ #in ...
- P1461 海明码 Hamming Codes
题目描述 给出 N,B 和 D,要求找出 N 个由0或1组成的编码(1 <= N <= 64),每个编码有 B 位(1 <= B <= 8),使得两两编码之间至少有 D 个单位 ...
随机推荐
- lua环境变量
function foo() print(g or "'g' is not defined!") end foo() env = { g = 100, print = print ...
- Mac Terminal 快捷键
在Mac系统中并没有Home.End等键,所以在使用时并不是特别的顺手,但是有几个键位组合可以使Terminal的操作更加灵活方便. 1.将光标移动到行首:ctrl + a 2.将光标移动到行尾:ct ...
- Absolute Horizontal And Vertical Centering In CSS
Quick CSS Trick: How To Center an Object Exactly In The Center Centering in CSS: A Complete Guide Ab ...
- linux网络路由配置
网卡配置文件介绍: # vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 (描述网卡对应的设备别名,例如ifcfg-eth0的文件中它为 ...
- 初步学习Axure---整理了一下自己两周的学习成果:动态面板
自己无意间发现了做原型设计的工具--Axure,所以就自学了一点皮毛.最近时间比较充裕,就把自己现学现卖的东西整一整. 作品比较简单,没有技术可言,根据用户和开发需求,利用动态面板和一些点击事件完成了 ...
- C# 取web应用程序运行目录
HttpRuntime.AppDomainAppPath
- 再谈json
接上一篇,省市三级联动的例子中,引入了1个QQ网站上的js文件.这个js中构造了一个地址对象,页面上我们所有的操作都跟这个对象关联.今天讨论这种对象怎么构造的问题. 前面写过一篇:浅谈Json数据格式 ...
- elasticsearch聚合函数
计算每个tag下的商品数量 GET /ecommerce/product/_search { "aggs": { //聚合 "group_by_tags": ...
- Bind for 0.0.0.0:80 failed: port is already allocated.解决方案
一句话总结就是容器占用的port还没有完全释放 查看进程,发现相关的容器并没有在运行,而 docker-proxy 却依然绑定着端口: $ docker ps 检查docker镜像 $ ps -aux ...
- Java 超类引用子类对象的示例代码
动态方法分配 dynamic method dispatch 一个被重写的方法的调用会在运行时解析,而不是编译时解析 Java 会根据在调用发生时引用的对象的类型来判断所要执行的方法 public c ...