题目链接
题目描述
In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In other words, it measures the minimum number of substitutions required to change one string into the other, or the minimum number of errors that could have transformed one string into the other.– Wikipedia
Assume that there are two strings with same length, which only contain lowercase characters, find a lexicographically smallest string with same length, and the Hamming distance between the target string and each original string are equal.
输入
The first line contains a number T (1≤T≤100), indicating the number of test cases.
Each time case contains two lines of strings, indicating the two original strings, which only contain lowercase characters. The length of string is smaller than 10^4, and total length of the strings is less then 10^6.
输出
For each case, output "Case x: y in which x indicates the case number starting with 1, and y indicates the result of the target string. The target string should only contain lowercase characters.
样例输入
复制样例数据
2
abc
acd
abandon
newyork
样例输出
Case 1: aaa
Case 2: aaaaark
【题意】
汉明码?字符串a和字符串b长度相等,其汉明码距离为 有多少个i满足a[i]!=b[i]
每组输入是长度相等的两个字符串s和t,请你求出一个字符串str,满足 str和s的汉明码距离 等于 str和t的汉明码距离。
存在多组解,则输出字典序最小的一个。
【分析】
很明显大多数时候填′ a ′ ‘a’ 会比较优秀,因为字典序的比较关系,只要这一位能填′ a ′ ‘a’ ,我们就应该无脑填′ a ′ ‘a’ ,我们记nowdis为a 距 离 -b距离 ,最后nowdis应该是0。明显当a , b 两个串字母相同时,本位不管放什么都不会影响nowdis。当a , b串不同时,我们既可以让nowdis不变,也可以nowdis++,也可以nowdis−−,所以这就是nowdis的松弛度,即为后缀中不同字母的数量。只要后缀字母的松弛度大于等于a b s ( n o w d i s ) abs(nowdis)abs(nowdis),我就放′ a ′ ‘a’ ,代码中其实也就是枚举每一个字母放而已.
#include<stdio.h>
#include

本文介绍如何通过算法解决给定字符串问题,即寻找两个输入字符串间的最小汉明距离对应字符串,使得两对原始字符串的汉明距离相等。通过实例演示和C++代码实现,探讨如何利用字典序和松弛度找到满足条件的字典序最小字符串。
最低0.47元/天 解锁文章
390

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



