`
hellobin
  • 浏览: 62952 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

UVA 729 - The Hamming Distance Problem

    博客分类:
  • UVA
 
阅读更多

The Hamming distance between two strings of bits (binary integers) is the number of corresponding bit positions that differ. This can be found by using XOR on corresponding bits or equivalently, by adding corresponding bits (base 2) without a carry. For example, in the two bit strings that follow:

                               A      0 1 0 0 1 0 1 0 0 0
                               B      1 1 0 1 0 1 0 1 0 0
                            A XOR B = 1 0 0 1 1 1 1 1 0 0

The Hamming distance (H) between these 10-bit strings is 6, the number of 1's in the XOR string.

 

Input 

Input consists of several datasets. The first line of the input contains the number of datasets, and it's followed by a blank line. Each dataset contains N, the length of the bit strings and H, the Hamming distance, on the same line. There is a blank line between test cases.

 

Output 

For each dataset print a list of all possible bit strings of length N that are Hamming distance H from the bit string containing all 0's (origin). That is, all bit strings of length N with exactly H 1's printed in ascending lexicographical order.

 


The number of such bit strings is equal to the combinatorial symbol C(N,H). This is the number of possible combinations of N-H zeros and H ones. It is equal to 

 

\begin{displaymath}{N!} \over {(N-H)! H!}
\end{displaymath}

 

 

This number can be very large. The program should work for $1 \le H \le N
\le 16$.

Print a blank line between datasets.

 

Sample Input 

 

1

4 2

 

Sample Output 

 

0011
0101
0110
1001
1010
1100

 

最后输出只能有一行空行,否则WA

 

#define RUN
#ifdef RUN

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <cctype> 
#include <algorithm>
#include <utility>
#include <math.h>
#include <ctime>

using namespace std;

#define MAXN 20

char buf[MAXN];
int N, H;
int len;

int main(){

#ifndef ONLINE_JUDGE
	freopen("729.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n;
	scanf("%d", &n);

	while(n--){
		scanf("%d%d", &N, &H);

		len = 0; 
		//N-H zeros and H ones
		memset(buf, 0, sizeof(buf));
		for(int i=0; i<N-H; i++){
			buf[len++] = '0';
		}
		for(int i=0; i<H; i++){
			buf[len++] = '1';
		}
		

		do {
			printf("%s\n", buf);
		} while ( next_permutation(buf, buf+len) );

		if(n != 0){
			printf("\n");
		}
			
	}

}


#endif

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics