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 个单位 ...
随机推荐
- 获取远程请求的IP地址、本机Mac地址和客户端Mac地址
我在近期项目里面去记录异常日志时,用到了这两个地址,也是从网上和前辈那里学习到的,本人项目是MVC框架的,自己整理了一个公共方法类,包括获取远程客户端IP和Mac地址,以及获取本机Mac地址的方法,代 ...
- Java电子书下载地址
http://www.itpub.net/search.php?searchid=1660&orderby=lastpost&ascdesc=desc&searchsubmit ...
- (转载)RxJava 与 Retrofit 结合的最佳实践
RxJava 与 Retrofit 结合的最佳实践 作者:tough1985 感谢 DaoCloud 为作者提供的 500 RMB 写作赞助: 成为赞助方 /开始写作 前言 RxJava和Retrof ...
- 第八周读书笔记(人月神话X月亮与六便士)——到底什么才是一个程序员的自我修养?
写了这么久的读书笔记,涉及到问题大多是一些如何把软件工程做好,如何把自己的职业生涯做好.但总感觉逻辑链上缺了一环,亦即:我们为什么要把软件工程做好,我们成为一名优秀的职业生涯的意义到底在于什么?我觉得 ...
- TD中INPUT的宽度设置
最近在用C#做Web程序时,碰到一个问题: 将TextBox的asp控件放在Table中的TD里面时,不设置宽度,自动会将TD撑的很大. 查看运行后页面的源代码发现,其实TextBox控件最终在客户端 ...
- ASP.NET 微信公众平台模板消息推送功能完整开发
最近公众平台的用户提出了新需求,他们希望当收到新的邮件或者日程的时候,公众平台能主动推送一条提醒给用户.看了看平台提供的接口,似乎只有[模板消息]能尽量满足这一需求,但不得不说微信提供的实例太少,而且 ...
- CodeIgniter + smarty 实现widget功能
在开发过程中,经常需要widget功能,一可以隔离页面逻辑,二可以重用代码.结合smarty的plugin功能,可以方便的实现该功能. 譬如,我们的页面中可以这样写: {{extends file=' ...
- eclipse离线安装pydev
首先,下载去http://pydev.org/下载Python的Eclipse插件PyDev. 目前的最新版是PyDev 2.7.1.zip,将压缩文件解压出来.得到features和plugins两 ...
- 前端开发—Javascript
Javascript 语言简介: 语言规范: 注释:/ 这是单行注释 / /* 换行*/ 多行注释 结束符: :分号 语法基础 变量 变量声明 1 变量名可以是 数字 字母 下划线 $ 组成,不 ...
- Flask-SQLAlchemy中解决数据库连接1366报错
报错信息:Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'V ...