一、题目
 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 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
二、分析

STL中的next_permutation应用。生成全排列。


三、AC源代码
 1 #include <iostream>
2 #include <algorithm>
3 #include <string.h>
4 using namespace std;
5 int main()
6 {
7 int i,n,a,b;
8 char str[20];
9 cin>>n;
10 while(n--)
11 {
12 cin>>a>>b;
13 memset(str,0,sizeof(str));
14 for(i=0;i<a-b;i++)
15 str[i]='0';
16 for(i=a-b;i<a;i++)
17 str[i]='1';
18 do
19 {
20 cout<<str<<endl;
21 }while(next_permutation(str,str+strlen(str)));
22
23 if(n) cout<<endl;
24 }
25 return 0;
26 }

posted on 2011-12-13 23:48  小狗狗ACM  阅读(315)  评论(0编辑  收藏  举报