📌  相关文章
📜  在 M 次迭代后通过将所有 01 或 10 转换为 11 来查找二进制字符串

📅  最后修改于: 2022-05-13 01:56:06.530000             🧑  作者: Mango

在 M 次迭代后通过将所有 01 或 10 转换为 11 来查找二进制字符串

给定一个大小为N的二进制字符串str[]和一个整数M 。可以通过将所有0翻转为1来修改此二进制字符串,其中恰好有一个1作为邻居。任务是在M次这样的迭代之后找到二进制字符串的最终状态。
注: 2≤N≤10 3 ,1≤M≤10 9

例子:

方法:解决方案是基于观察到的修改可以进行不超过N次迭代,因为即使在每次迭代中,至少翻转一个0 ,那么它最多会进行N次,如果没有零是在迭代中翻转,则这意味着二进制字符串保持与上一步相同的状态并且模拟结束。因此,总的迭代次数将最小为NM。请按照以下步骤解决问题:

  • 初始化变量N并将其值设置为二进制字符串str的长度。
  • 将 M 的值设置为NM的最小值
  • 在外部 while 循环中迭代,直到M大于 0。
  • 初始化字符串s1=””以存储当前迭代后的修改版本。
  • 在内部 for 循环中从i=0迭代到i
  • 检查给定二进制字符串str[]第 i 个索引处的字符。
  • 如果str[i]=='1',则将此字符添加到二进制字符串s1。
  • 否则,检查它在i-1i+1 索引中的相邻字符。
  • 如果恰好有一个1 ,则将1添加到二进制字符串s1。
  • 否则,将0添加到二进制字符串s1。
  • 在内循环之后,检查二进制字符串strs1是否相同。
  • 如果是,则打破外循环。
  • 否则,将二进制字符串s1设置为二进制字符串str的新值,并将M的值减 1。
  • 最后,打印二进制字符串s1。

下面是上述方法的实现。

C++
// C++ program for the above approach.
#include 
using namespace std;
 
// Function to find the modified
// binary string after M iterations
void findString(string str, int M)
{
    int N = str.length();
 
    // Set the value of M to the minimum
    // of N or M.
    M = min(M, N);
 
    // Declaration of current string state
    string s1 = "";
 
    // Loop over M iterations
    while (M != 0) {
 
        // Set the current state as null
        // before each iteration
        s1 = "";
 
        for (int i = 0; i < N; i++) {
 
            if (str[i] == '0') {
 
                // Check if this zero has exactly
                // one 1 as neighbour
                if ((str[i - 1] == '1'
                     && str[i + 1] != '1')
                    || (str[i - 1] != '1'
                        && str[i + 1] == '1'))
 
                    // Flip the zero
                    s1 += '1';
 
                else
                    s1 += '0';
            }
            else
                s1 += '1';
        }
 
        // If there is no change,
        // then no need for
        // further iterations.
        if (str == s1)
            break;
 
        // Set the current state
        // as the new previous state
        str = s1;
        M--;
    }
 
    cout << s1;
}
 
// Driver Code
int main()
{
    // Given String
    string str = "0110100";
 
    // Number of Iterations
    int M = 3;
 
    // Function Call
    findString(str, M);
 
    return 0;
}


Java
// Java program for the above approach.
import java.io.*;
import static java.lang.Math.min;
import java.lang.*;
 
class GFG
{
   
// Function to find the modified
// binary string after M iterations
public static void findString(String str, int M)
{
    int N = str.length();
 
    // Set the value of M to the minimum
    // of N or M.
    M = Math.min(M, N);
 
    // Declaration of current string state
    String s1 = "";
 
    // Loop over M iterations
    while (M != 0) {
 
        // Set the current state as null
        // before each iteration
        s1 = "";
 
        for (int i = 0; i < N; i++) {
 
            if (str.charAt(i) == '0') {
 
                // Check if this zero has exactly
                // one 1 as neighbour
                if ((str.charAt(i) == '1'
                     && str.charAt(i) != '1')
                    || (str.charAt(i) == '1'
                        && str.charAt(i) == '1'))
 
                    // Flip the zero
                    s1 += '1';
 
                else
                    s1 += '0';
            }
            else
                s1 += '1';
        }
 
        // If there is no change,
        // then no need for
        // further iterations.
        if (str == s1)
            break;
 
        // Set the current state
        // as the new previous state
        str = s1;
        M--;
    }
 
    System.out.print(s1);
}
 
// Driver Code
public static void main (String[] args)
{
   
    // Given String
    String str = "0110100";
 
    // Number of Iterations
    int M = 3;
 
    // Function Call
    findString(str, M);
 
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python 3 program for the above approach.
 
# Function to find the modified
# binary string after M iterations
def findString(str,M):
    N = len(str)
 
    # Set the value of M to the minimum
    # of N or M.
    M = min(M, N)
 
    # Declaration of current string state
    s1 = ""
 
    # Loop over M iterations
    while (M != 0):
       
        # Set the current state as null
        # before each iteration
        s1 = ""
 
        for i in range(N-1):
            if (str[i] == '0'):
               
                # Check if this zero has exactly
                # one 1 as neighbour
                if ((str[i - 1] == '1' and str[i + 1] != '1') or (str[i - 1] != '1' and str[i + 1] == '1')):
                    # Flip the zero
                    s1 += '1'
 
                else:
                    s1 += '0'
            else:
                s1 += '1'
 
        # If there is no change,
        # then no need for
        # further iterations.
        if (str == s1):
            break
        s1 += '1'
 
        # Set the current state
        # as the new previous state
        str = s1
        M -= 1
 
    print(s1)
 
# Driver Code
if __name__ == '__main__':
   
    # Given String
    str = "0110100"
 
    # Number of Iterations
    M = 3
 
    # Function Call
    findString(str, M)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach.
using System;
class GFG {
     
    // Function to find the modified
    // binary string after M iterations
    static void findString(string str, int M)
    {
        int N = str.Length;
      
        // Set the value of M to the minimum
        // of N or M.
        M = Math.Min(M, N);
      
        // Declaration of current string state
        string s1 = "";
      
        // Loop over M iterations
        while (M != 0) {
      
            // Set the current state as null
            // before each iteration
            s1 = "";
      
            for (int i = 0; i < N; i++) {
                 
                if (str[i] == '0')
                {                                                       
                    // Check if this zero has exactly
                    // one 1 as neighbour
                    if (((i>0 && str[i - 1] == '1') && (i0 && str[i - 1] != '1') && (i


Javascript


输出
1110111

时间复杂度: O(min(M, N)*N)
辅助空间: O(1)