2018CCPC桂林站题解——H. Hamming Distance

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

题目链接
题目描述

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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小编程员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值