


The Hamming distance between two strings of equal length is the number of all positions where different values exist at the corresponding positions. We can understand through the following example:
S= “ramanisgoing”
’s Chinese translation is:S= “ramanisgoing”
T=“dishaisgoing”
Here, 5 is the Hamming distance between the two strings S and T, since raman and disha are two words that make the difference in the strings equal.
Problem Statement
However, in this problem, we need to find the Hamming distance between two strings containing only binary digits. One string will be provided by the user, let's say S, and another string, let's say T. Initially, we assume it contains only '0' bits and is equal to the size of the given string. We will get a number 'k' whose value represents the number of elements a substring can consist of only '1's as its elements so that we place that substring of size k in the string(T) Any position that minimizes the Hamming distance between two substrings S and T.
Let's try to understand this problem through some examples.
Input − S = "100111" K = 5
Output - 3
Explanation − The initial string T is equal to "000000", and the string T will be changed to compare with the string S to find the minimum Hamming distance when k=5, as shown below : "111110" and "011111".
The Hamming distance between 100111 and 000000 is 4. The Hamming distance between 100111 and 111110 is 3, and the Hamming distance between 100111 and 011111 is also 3.
But the minimum Hamming distance will be 3, because 3 is less than 4. Therefore, our answer is 3.
- S = "100101" K = 5
- 3
− As the initial string T will be equal to "000000", and the string T will be changed to compare with the string S to find the minimum Hamming distance when k=5, as follows Display: "111110" and "011111".
The Hamming distance between 100101 and 000000 is 3. The Hamming distance between 100101 and 111110 is 4, and the Hamming distance between 100101 and 011111 is also 4.
But the minimum Hamming distance will be 3, because 3 is less than 4. Therefore, our answer is 3.
Explanation of the problem
Let's try to understand this problem and find a solution.
Solution 1 Brutal solution
We will modify the string T by changing the positions of substrings with different initial and end points in order to obtain the smallest Hamming distance among all possible strings.
Example
The following is the C program implementation of the above method:
#include <bits/stdc++.h> using namespace std; // Make a function to get minimum hamming distance through iteration int helper(string S,int k){ // n is the size of the string int n=S.size(); // Take another string T and initiate it with zero bits size equal to that of S string T; for(int i=0;i<n;i++){ T+="0"; } // Take another string v to initiate it same as T string v=T; // Define mini as the hamming distance between T and S int mini=0; int l=0; while(l<n){ if(S[l]!=T[l])mini++; l++; } for(int i=0;i<n-k+1;i++){ int j=0,a=0,l=0; // alter string v by changing bits of size k while(j<k){ v[j+i]='1'; j++; } // calculate hamming distance while(l<n){ if(S[l]!=v[l])a++; l++; } // Check if the previous hamming distance is greater than the current hamming distance, if yes then replace that distance element if(mini>a){ mini=a; } // Again assign v as the T string v=T; } // return the minimum hamming distance found through the above iterations return mini; } int main() { // Give input string S string S = "100101"; // Give the value of k that is the substring size int K = 5; // Call the helper function cout << "The minimum hamming distance is: "<< helper(S,K); return 0; }
Output
The minimum hamming distance is: 3
Solution 2 Optimization plan
algorithm
Use the prefix sum array to calculate the number of 1's and store it as our minimum Hamming distance
Traverse the string S to find the values between K different substrings in the string S.
If (i-1
Store the minimum value by finding the minimum value between the previous Hamming distance and the current Hamming distance.
The current Hamming distance can be found by operating on the number of zero elements in the (K - v) substring and the number of zeros in the current S substring
Finally, return the overall minimum distance.
Example
The following is the C program implementation of the above method
#include <bits/stdc++.h> using namespace std; // Make a helper function to get minimum hamming distance through iteration int helper(string S, int K){ // n is the size of the string int n = S.size(); // initialize an array of size 'n' int arr[n]; if(S[0]=='0')arr[0]=0; else arr[0]=1; // Count the number of 1's in the string S for (int i = 1; i < n; i++){ if(S[i]=='0')arr[i]=arr[i-1]; else arr[i]=arr[i-1]+1; } int cnt = arr[n - 1]; // Define mini as the hamming distance between T and S int mini = cnt; // Traverse through S to find the minimum for (int i = 0; i < n - K; i++) { int v; if(i-1==-1)v=arr[i+K-1]; else v= arr[i+K-1]-arr[i-1]; // Store the minimum mini = min(mini, cnt - v + (K - v)); } // Return the minimum hamming distance return mini; } int main(){ // Give input string S string S = "100101"; // Give the value of k that is the substring size int K = 5; // Call the helper function cout << "The minimum hamming distance is: "<< helper(S,K); return 0; }
Output
The minimum hamming distance is: 3
in conclusion
In this article, to find the minimum Hamming distance, we will first see a simple method, but to improve its time complexity, we will use the concept of prefix and array, through which we can to avoid double counting.
The above is the detailed content of Minimize the Hamming distance of a binary string by setting a substring containing only K bits. For more information, please follow other related articles on the PHP Chinese website!

MySQL中如何使用LOCATE函数查找子字符串在字符串中的位置在MySQL中,有许多函数可以用来处理字符串。其中,LOCATE函数是一种非常有用的函数,可以用来查找子字符串在字符串中的位置。LOCATE函数的语法如下:LOCATE(substring,string,[position])其中,substring为要查找的子字符串,string为要在其中

该函数与strtok()函数类似。唯一的关键区别是_r,它被称为可重入函数。可重入函数是在执行过程中可以被中断的函数。这种类型的函数可用于恢复执行。因此,可重入函数是线程安全的,这意味着它们可以安全地被线程中断,而不会造成任何损害。strtok_r()函数有一个称为上下文的额外参数。这样函数就可以在正确的位置恢复。strtok_r()函数的语法如下:#include<string.h>char*strtok_r(char*string,constchar*limiter,char**

二进制算法是一种基于二进制数的运算方法,其基本运算包括加法、减法、乘法和除法。除了基本运算外,二进制算法还包括逻辑运算、位移运算等操作。逻辑运算包括与、或、非等操作,位移运算包括左移和右移操作。这些操作都有对应的规则和操作数的要求。

二进制数以1和0表示。16位的十六进制数系统为{0,1,2,3…..9,A(10),B(11),……F(15)}为了从二进制表示转换为十六进制表示,位串id被分组为4位块,从最低有效侧开始称为半字节。每个块都替换为相应的十六进制数字。让我们看一个示例,以清楚地了解十六进制和二进制数字表示。001111100101101100011101 3 E 5 B&nb

EDVAC的两个重大的改进:一是采用二进制,二是完成了存贮程序,可以自动地从一个程序指令进到下一个程序指令,其作业可以通过指令自动完成。“指令”包括数据和程序,把它们用码的形式输入到机器的记忆装置中,即用记忆数据的同一记忆装置存贮执行运算的命令,这就是所谓存贮程序的新概念。

Golang能否处理二进制文件?在Go语言中,处理二进制文件是非常常见且方便的。通过使用内置的包和方法,我们可以轻松地读取、写入和操作二进制文件。本文将介绍如何在Go中处理二进制文件,并提供具体的代码示例。读取二进制文件要读取一个二进制文件,我们首先需要打开这个文件并创建一个对应的文件对象。然后,我们可以使用Read方法从文件中读取数据,并以字节的形式存储在

题目:轻松学会Go语言中16进制转二进制,需要具体代码示例在计算机编程中,经常会涉及到对不同进制数之间的转换操作。其中,16进制和二进制之间的转换是比较常见的。在Go语言中,我们可以通过一些简单的代码示例来实现16进制到二进制的转换,让我们一起来学习一下。首先,我们来了解一下16进制和二进制的表示方法。16进制是一种表示数字的方法,使用0-9和A-F来表示1

Golang如何读取二进制文件?二进制文件是以二进制形式存储的文件,其中包含了计算机能够识别和处理的数据。在Golang中,我们可以使用一些方法来读取二进制文件,并将其解析成我们想要的数据格式。下面将介绍如何在Golang中读取二进制文件,并给出具体的代码示例。首先,我们需要使用os包中的Open函数打开一个二进制文件,这将返回一个文件对象。然后,我们可以使


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
