您的位置:首页 > 其它

UVa729 The Hamming Distance Problem

2013-07-18 16:11 417 查看


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

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 of1's in the XOR string.

Input 

Input consists of several datasets. The first line of the input contains the number of datasets, and it'sfollowed by a blank line. Each dataset containsN, 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 printa list of all possible bit strings of length N that areHamming distance
Hfrom the bit string containing all 0's (origin). That is, all bit stringsof length
N with exactly H 1's printed in ascending lexicographicalorder.

The number of such bit strings is equal to the combinatorialsymbol C(N,H). This is the number of possible combinations of
N-H zerosand H ones. It is equal to



This number can be very large. The program should work for

.

Print a blank line between datasets.

Sample Input 

1

4 2


Sample Output 

0011
0101
0110
1001
1010
1100


Miguel Revilla

2000-08-31

这道题大意其实就是求所给定N位数字的全部组合数,输入的N是要排列数字的个数,H是其中1的数目,这题直接从N-H个0和H个1所能构造的最小数字开始,用next_permutation()生成所有排列即可。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

const int N = 25;
char s
;

int main(){
int t;
cin >> t;
while (t--){
memset(s,0,sizeof(s));
int n;
int h;
cin >> n;
cin >> h;
for (int i = 0; i < n-h; i++)
s[i] = '0';
for (int i = n-h; i < n; i++)
s[i] = '1';
cout << s << endl;
while (next_permutation(s,s+n))
cout << s << endl;
if (t > 0)
cout << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva 算法