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 个单位 ...
随机推荐
- Ubuntu 完全卸载MySQL 重装步骤
sudo rm /var/lib/mysql/ -R 删除mysql的数据文件 sudo rm /etc/mysql/ -R 删除mqsql的配置文件 sudo apt-get autorem ...
- Python下划线的使用
References: [1]. http://python.jobbole.com/81129/ 本文将讨论Python中下划线(_)字符的使用方法.我们将会看到,正如Python中的很多事情,下划 ...
- 测试class
各种断言方法: assertEqual(a,b) a == b assertNotEqual(a,b) a != b assertTrue(x) x == True assertFalse(x) x ...
- Spark踩坑记——从RDD看集群调度
[TOC] 前言 在Spark的使用中,性能的调优配置过程中,查阅了很多资料,之前自己总结过两篇小博文Spark踩坑记--初试和Spark踩坑记--数据库(Hbase+Mysql),第一篇概况的归纳了 ...
- 刨根究底字符编码之八——Unicode编码方案概述
Unicode编码方案概述 1. 前面讲过,随着计算机发展到世界各地,于是各个国家和地区各自为政,搞出了很多既兼容ASCII但又互相不兼容的各种编码方案.这样一来同一个二进制编码就有可能被解释成不 ...
- 《算法4》2.1 - 插入排序算法(Insertion Sort), Python实现
排序算法列表电梯: 选择排序算法:详见 Selection Sort 插入排序算法(Insertion Sort):非常适用于小数组和部分排序好的数组,是应用比较多的算法.详见本文 插入排序算法的语言 ...
- VR上天了!全景商业化落地了!——VR全景智慧城市
几年前,VR创业公司SpaceVR就启动了旨在将宇航员视觉体验带给普通人的虚拟现实(VR)项目.SpaceVR计划将VR相机卫星送入太空,并将相机拍摄到的太空视频发送回地球,从而让VR用户身临其境地看 ...
- loadrunner提高篇-结果分析实践
分析图合并 一.分析图合并原理 选择view->merge graphs,弹出如图1所示对话框 图1(设置合并图) 1.选择要合并的图.选择一个要与当前活动图合并的图,注意这里只能选择X轴度量单 ...
- {网络编程}和{多线程}应用:基于UDP协议【实现多发送方发送数据到同一个接收者】--练习
要求: 使用多线程实现多发送方发送数据到同一个接收者 代码: 发送端:可以在多台电脑上启动发送端,同时向接收端发送数据 注意:匹配地址和端口号 package com.qf.demo; import ...
- asp.net MVC 网站图片防盗链的几种方法
目录 1. 通过 URL Rewrite Module 组件 2. 通过 nginx 图片防盗链 3.自定义 HttpHandler 处理 4. 通过 MVC 自定义路由规则防盗链 5. 通过 MVC ...