The Hamming Distance Problem UVA729
程序员文章站
2024-03-19 09:16:34
...
The Hamming Distance Problem |
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 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 print a list of all possible bit strings of lengthNthat are Hamming distanceHfrom the bit string containing all 0's (origin). That is, all bit strings of lengthNwith exactlyH1's printed in ascending lexicographical order.
The number of such bit strings is equal to the combinatorial symbolC(N,H). This is the number of possible combinations ofN-Hzeros andHones. 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
给出两个数,一个是总位数,另一个是1的位数,求全排列
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
int n,k;
cin>>n;
for(k=1;k<=n;k++)
{
int r,t,i,j;
cin>>r>>t;
string str;
for(i=1;i<=r-t;i++)
str=str+'0';
for(i;i<=r;i++)
str=str+'1';
cout<<str<<endl;
while(next_permutation(str.begin(), str.end()))
{
cout<<str<<endl;
}
if(k!=n) cout<<endl;
}
return 0;
}