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 个单位 ...
随机推荐
- mybatis 高级映射和spring整合之高级映射(4)
mybatis 高级映射和spring整合之高级映射 ----------------学习结构-------------------- 0.0 对订单商品数据模型进行分析 1.0 高级映射 1.1 一 ...
- ubuntu16 mysql 远程连接
打开配置文件: sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf 将下面一行注释掉: # bind-address = localhost 重启服务 sudo s ...
- GEF入门笔记
最近项目中需要用到Eclipse GEF框架进行画图,故将平时学习笔记更新到博客中,便于查阅 自己画的一个GEF基本结构 最基本流程 1.创建model(包括数据域.在界面中的布局.图片索引等 ...
- Thread pool引起的程序连接数据库响应慢
数据库版本:percona-mysql 5.6.16 在很长一段时间,都会出现程序连接数据库,出现响应慢的情况,正常在几到几十毫秒之间,但是偶尔会出现上百毫秒的情况: 开始由于开发重新设置并调整过程 ...
- matplotlib简介-高质量图形输出
Matplotlib 是一个用来绘制二维图形的 Python 模块,它克隆了许多 Matlab 中的函数, 用以帮助 Python 用户轻松获得高质量(达到出版水平)的二维图形. 文章来源:http: ...
- 【领略RxSwift源码】- 变换操作(Operators)
在上一篇中,我们分析了在RxSwift中的整个订阅流程.在开讲变换操作之前,首先要弄清楚Sink的概念,不清楚的同学可以翻看上一篇的分析.简单的来说,在每一次订阅操作之前都会进行一次Sink对流的操作 ...
- Select, Poll,Epoll
Date: 2019-06-19 Author: Sun 1. Select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当se ...
- day27-1 numpy模块
目录 numpy array 一维数组 二维数组(用的最多) np.array和list的区别 获取多维数组的行和列 多维数组的索引 高级功能 多维数组的元素替换 多维数组的合并 通过函数方法创建多维 ...
- http请求后台报406错误
1.springMVC的项目,通过浏览器访问后台方法遇到了报406的错误,找了很多原因,最终发现是因为缺少spring-mvc的json配置. 2.添加依赖:jackson-databind.jack ...
- CentOS 笔记(六) 历史命令 自动补充
history #出现历史的命令 #执行具体的一条历史命令 !458 ②安装自动补充功能 yum install -y bash-completion #输入命令 按Tab 会显示所有内容 syste ...