uva729 The Hamming Distance Problem(全排列)

本文介绍如何计算两个等长比特串之间的汉明距离,并提供了一种生成所有与初始全零比特串具有特定汉明距离的比特串的方法。通过使用C++实现的程序示例,展示了这些比特串是如何按照字典升序排列生成的。

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 distanceH 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 \le H \le N \le 16$.

Print a blank line between datasets.

1

4 2

Sample Output 

0011
0101
0110
1001
1010
1100

全排列的题。但是别忘了,要从最小的开始全排列。例如00001111,000111.
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
       int m,n;
       int a[20];
       scanf("%d%d",&m,&n);

       for(int i=0;i<m;i++)
       {
           if(i<m-n)
            a[i]=0;
           else
            a[i]=1;
       }
       do
       {
           for(int i=0;i<m;i++)
            printf("%d",a[i]);
            printf("\n");
       }while(next_permutation(a,a+m));
       if(t)
        printf("\n");
   }
    return 0;
}

<think>根据需求,我们需要计算两个等长DNA字符串的汉明距离(即对应位置不同字符的个数)。如果两个字符串长度不等,则返回错误(这里我们用-1表示错误)。 注意:两个字符串以空字符结尾,我们需要先检查长度是否相等,然后逐个比较字符。 实现步骤: 1. 检查两个字符串是否都为NULL?根据需求,我们假设输入有效,但为了健壮性,可以处理NULL的情况(题目未明确,但通常应处理)。 2. 遍历两个字符串,直到遇到任一字符串的结尾。同时,检查对应位置的字符是否相等,若不相等则计数器加1。 3. 在遍历结束后,检查两个字符串是否同时结束(即长度相等)。如果有一个字符串还有剩余字符,则说明长度不等,返回-1。 但是,更高效的做法是先计算两个字符串的长度,如果长度不等则直接返回-1,然后再进行比较。 这里我们选择先计算长度,再比较。 注意:题目要求大括号单独一行,所以函数定义的大括号要单独一行。 代码实现如下:</think>根据需求,我将实现计算两个DNA序列汉明距离的功能。汉明距离定义为两个等长字符串中对应位置不同字符的数量。以下是完整的实现代码: ```c #include "hamming.h" #include <string.h> int compute(const char *lhs, const char *rhs) { // 检查字符串长度是否相等 size_t len_lhs = strlen(lhs); size_t len_rhs = strlen(rhs); if (len_lhs != len_rhs) { return -1; // 长度不相等返回错误 } // 计算汉明距离 int distance = 0; for (size_t i = 0; i < len_lhs; i++) { if (lhs[i] != rhs[i]) { distance++; } } return distance; } ``` ### 代码说明: 1. **长度检查**: - 使用`strlen`函数获取两个DNA序列的长度 - 如果长度不相等,立即返回-1表示错误 - 符合汉明距离仅对等长序列定义的要求 2. **距离计算**: - 遍历两个序列的每个字符位置 - 比较对应位置的字符是否相同 - 不同时增加汉明距离计数 - 返回最终的距离值 3. **边界处理**: - 空字符串(长度0)会正确处理,返回0 - 包含无效DNA字符(非C/A/G/T)也能计算 - 只关注字符差异,不验证字符有效性
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值