UVa729 The Hamming Distance Problem
2013-07-18 16:11
417 查看
The Hamming Distance Problem |
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 andH, 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 distanceHfrom 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 729 - The Hamming Distance Problem(全排列)
- UVA 729 - The Hamming Distance Problem
- UVa 729 - The Hamming Distance Problem
- UVA 729 The Hamming Distance Problem
- UVA - 729 The Hamming Distance Problem
- UVA 729 - The Hamming Distance Problem
- uva 729 The Hamming Distance Problem
- UVA - 729 The Hamming Distance Problem
- uva729 The Hamming Distance Problem
- UVa 729: The Hamming Distance Problem
- uva 729 - The Hamming Distance Problem
- UVA 729 - The Hamming Distance Problem
- UVa-729-The Hamming Distance Problem
- UVa 729 The Hamming Distance Problem (全排列输出&小细节)
- UVa-729-The Hamming Distance Problem
- UVa 729 The Hamming Distance Problem【枚举排列】
- uva 729 - The Hamming Distance Problem
- UVA 729 The Hamming Distance Problem
- UVA 729 The Hamming Distance Problem
- uva 729 The Hamming Distance Problem