USACO Section 2.1 Hamming Codes
来源:互联网 发布:大数据职称 编辑:程序博客网 时间:2024/05/16 15:21
题目原文
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 0100Bit 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 7682 85 97 102 120 127
分析
int getHammingDistance(int a,int b){int dis=0;for (int i=0;i!=B;i++){if((1<<i & a) != (1<<i & b))dis++;}return dis;}
提交代码
/*ID: PROG: hammingLANG: C++*/#include <iostream>#include <fstream>#include <vector>#include <algorithm>#include <string>#include <math.h>#include <limits>#include <map>using namespace std;int N,B,D;int getHammingDistance(int a,int b){int dis=0;for (int i=0;i!=B;i++){if((1<<i & a) != (1<<i & b))dis++;}return dis;}int main(){ifstream cin("hamming.in");ofstream cout("hamming.out");cin >> N >> B >> D;vector<int> codes;codes.push_back(0);int temp=1;int count=1;while(count<N && temp<pow(2.0,B)){bool flag = true;for (int i=0;i!=codes.size();i++){flag = flag && (getHammingDistance(temp,codes[i])>=D);if(!flag)break;}if(flag){codes.push_back(temp);count++;}temp++;}for (int i=0;i!=codes.size()-1;i++){cout << codes[i];if(i%10==9)cout <<endl;elsecout << " ";}cout << codes[codes.size()-1] << endl;return 0;}
提交结果
TASK: hammingLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.011 secs, 3500 KB] Test 2: TEST OK [0.008 secs, 3500 KB] Test 3: TEST OK [0.005 secs, 3500 KB] Test 4: TEST OK [0.008 secs, 3500 KB] Test 5: TEST OK [0.005 secs, 3500 KB] Test 6: TEST OK [0.005 secs, 3500 KB] Test 7: TEST OK [0.008 secs, 3500 KB] Test 8: TEST OK [0.008 secs, 3500 KB] Test 9: TEST OK [0.011 secs, 3500 KB] Test 10: TEST OK [0.005 secs, 3500 KB] Test 11: TEST OK [0.003 secs, 3500 KB]All tests OK.
官方参考答案
#include <stdio.h>#include <stdlib.h>#include <iostream.h>#define MAX (1 << 8 + 1)#define NMAX 65#define BMAX 10#define DMAX 10int nums[NMAX], dist[MAX][MAX];int N, B, D, maxval;FILE *in, *out;void findgroups(int cur, int start) { int a, b, canuse; char ch; if (cur == N) { for (a = 0; a < cur; a++) { if (a % 10) fprintf(out, " "); fprintf(out, "%d", nums[a]); if (a % 10 == 9 || a == cur-1) fprintf(out, "\n"); } exit(0); } for (a = start; a < maxval; a++) { canuse = 1; for (b = 0; b < cur; b++) if (dist[nums[b]][a] < D) { canuse = 0; break; } if (canuse) { nums[cur] = a; findgroups(cur+1, a+1); } }}int main() { in = fopen("hamming.in", "r"); out = fopen("hamming.out", "w"); int a, b, c; fscanf(in, "%d%d%d", &N, &B, &D); maxval = (1 << B); for (a = 0; a < maxval; a++) for (b = 0; b < maxval; b++) { dist[a][b] = 0; for (c = 0; c < B; c++) if (((1 << c) & a) != ((1 << c) & b)) dist[a][b]++; } nums[0] = 0; findgroups(1, 1); return 0;}
THE END
- USACO Section 2.1 Hamming Codes
- USACO Section 2.1 Hamming Codes
- USACO-Section 2.1 Hamming Codes (DFS)
- Section 2.1 Hamming Codes
- USACO section 2.1 Hamming Codes(直接枚举暴搜)
- USACO 2.1 Hamming Codes (hamming)
- USACO--2.1Hamming Codes
- USACO 2.1 Hamming Codes
- USACO 2.1 Hamming Codes
- USACO Hamming Codes 2.1
- USACO 2.1.5 Hamming Codes
- [USACO 2.1.5] Hamming Codes
- usaco 2.1.5 Hamming Codes
- USACO 2.1.5 Hamming Codes
- USACO 2.1 Hamming Codes (递归)
- USACO 2.1.5 Hamming Codes
- USACO Section 2.1 Hamming Codes - 题意相当坑爹..很无聊..
- USACO:2.1.5 Hamming Codes 汉明码
- C/C++:C++类的继承
- 数据结构第二章【思维导图】
- Android API翻译,DexClassLoader类的API中文翻译
- UVa 11059 - Maximum Product
- 题目:输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个 子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为 O(n)。
- USACO Section 2.1 Hamming Codes
- ASP.NET动态网站开发培训-16.DataList控件(三)
- 定义栈的数据结构,要求添加一个 min 函数,能够得到栈的最小元素。要求函数 min、push 以及 pop 的时间复杂度都是 O(1)。
- python 科学计算学习一:numpy文件存取,内存映射
- 斯坦福大学iOS应用开发教程学习笔记(第六课)故事版 StoryBoard
- ATL 属性为BSTR 时数据处理
- Maven实战(一)安装和配置
- 7.什么时候用assert。
- LCS, longest common sequence