分析
Hamming distance,汉明距离,举一些栗子。
"karolin" and "kathrin" is 3.
"karolin" and "kerstin" is 3.
1011101 and 1001001 is 2.
2173896 and 2233796 is 3.
题目给定字符串的长度N和汉明距离all 0的汉明距离为
其实只需要填充H个1,其余填充0,做全排列得解。
代码
#include <cstdio>
#include <algorithm>
using std::next_permutation;
char s[18];
int N, H;
void solve()
{
for (int i = 0; i < N-H; i++) s[i] = '0';
for (int i = N-H; i < N; i++) s[i] = '1';
s[N] = '\0';
do {
printf("%s\n", s);
} while (next_permutation(s, s+N));
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &N, &H);
solve();
if (T) printf("\n");
}
return 0;
}
题目
Description
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
This number can be very large. The program should work for 1≤H≤N≤16.
Print a blank line between datasets.
Sample Input
1
4 2
Sample Output
0011
0101
0110
1001
1010
1100
本文探讨了汉明距离的概念及其应用,并提供了一个求解特定条件下的全排列算法实例。通过实例分析,展示了如何计算两个字符串之间的汉明距离,并通过编程实现了一个生成指定汉明距离的全排列字符串的解决方案。
561

被折叠的 条评论
为什么被折叠?



