將給定二進位制字串中的所有 0 翻轉 K 次,且相鄰字元不同


在考慮相鄰字元的情況下翻轉二進位制字串中的 0 的任務在各個領域都有實際應用。在本教程中,我們將深入探討修改給定二進位制字串的問題,方法是重複翻轉具有不同相鄰字元的 0。具體來說,我們的目標是在 C++ 程式設計的上下文中解決這個問題。

該解決方案涉及迭代掃描字串並根據提供的邏輯應用必要的翻轉。透過利用字串操作功能,我們可以有效地透過翻轉 K 次 0 來轉換二進位制字串,確保每次翻轉都符合具有不同鄰居的標準。透過對問題陳述的深入檢查,並輔以 C、C++、Java 和 Python 的逐步實現,本教程提供瞭解決這個有趣的二進位制字串操作挑戰的綜合指南。讓我們開始吧!

問題陳述

給定一個二進位制字串和翻轉次數,任務是翻轉字串中所有出現的“0”,同時考慮相鄰字元。目標是將“0”更改為“1”,並且如果相鄰字元為“1”,則也將它們翻轉為“0”。

示例 1

輸入

binary string: "01001"; Number of flips: 4

輸出

Flipped string: 00011

解釋:初始二進位制字串為“01001”。應用翻轉後,所有“0”都轉換為“1”。如果相鄰字元為“1”,則也會翻轉它們。生成的翻轉字串為“00011”。

示例 2

輸入

binary string: 101010; Number of flips: 2

輸出

Flipped string: 010110

解釋 − 在這種情況下,初始二進位制字串為“101010”。由於字串中沒有“0”,因此無法應用翻轉。因此,生成的翻轉字串與輸入字串相同,即“010110”。

演算法

  • 首先定義 `flipZeroes` 函式,該函式以二進位制字串和翻轉次數作為輸入。

  • 建立二進位制字串的副本,並將其儲存在 `result` 變數中。

  • 確定二進位制字串的長度,並將其儲存在變數 `n` 中。

  • 使用迴圈迭代字串,從索引 0 開始,直到到達字串末尾或翻轉次數變為 0。

  • 檢查當前字元是否為“0”。

  • 如果是,則透過將“1”賦值給 `result` 字串中的當前索引來將“0”翻轉為“1”。

  • 檢查相鄰字元(如果有)並在它們為“1”時翻轉它們。

  • 將剩餘翻轉次數減 1。

  • 返回生成的翻轉字串。

  • 在主函式中,提供示例二進位制字串和翻轉次數。

  • 使用二進位制字串和翻轉次數作為引數呼叫 `flipZeroes` 函式。

  • 輸出原始二進位制字串、翻轉次數和生成的翻轉字串。

總的來說,該程式允許翻轉二進位制字串中的“0”,同時考慮相鄰字元,並顯示原始字串、翻轉次數和生成的翻轉字串的輸出。

示例

以上演算法在各種程式語言中的實現

下面的程式將二進位制字串作為輸入,並在字串上執行指定的翻轉次數。`flipZeroes` 函式迭代字串,將“0”翻轉為“1”,並檢查相鄰字元以在可能的情況下翻轉它們。`flips` 變數確定要執行的最大翻轉次數。然後,程式輸出原始二進位制字串、翻轉次數和生成的翻轉字串。

注意 − 程式假設輸入字串僅包含 0 和 1。如果輸入字串包含其他字元,則程式可能無法按預期工作。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Function to flip '0's in a binary string
char* flipZeroes(const char* binaryString, int flips) {
   char* result = strdup(binaryString);
   int n = strlen(binaryString);
    
   // Loop through the string
   for (int i = 0; i < n && flips > 0; ++i) {
      // Check if the current character is '0'
      if (result[i] == '0') {
         // Flip the '0' to '1'
         result[i] = '1';
         
         // Check the neighbors and flip them if possible
         if (i - 1 >= 0 && result[i - 1] == '1') {
             result[i - 1] = '0';
         } else if (i + 1 < n && result[i + 1] == '1') {
             result[i + 1] = '0';
         }
         --flips;  // Decrease the number of remaining flips
      }
   }
   return result;
}

int main() {
   const char* binaryString = "01001";
   int flips = 4;
   printf("Input binary string: %s\n", binaryString);
   printf("Number of flips: %d\n", flips);
   char* result = flipZeroes(binaryString, flips);
   printf("Flipped string: %s\n", result);
   free(result); // Don't forget to free the allocated memory
   return 0;
}

輸出

Input binary string: 01001
Number of flips: 4
Flipped string: 00011
#include <iostream>
#include <string>

std::string flipZeroes(const std::string& binaryString, int flips) {
   std::string result = binaryString;
   int n = binaryString.length();
   // Loop through the string
   for (int i = 0; i < n && flips > 0; ++i) {
      // Verify whether the current character is equal to '0'
      if (result[i] == '0') {
         // Change the current '0' to '1' by flipping it
         result[i] = '1';
         // Check the neighbours and flip them if possible
         if (i - 1 >= 0 && result[i - 1] == '1') {
            result[i - 1] = '0';
         } else if (i + 1 < n && result[i + 1] == '1') {
            result[i + 1] = '0';
         }
         --flips;  // Decrease the number of remaining flips
      }
   }
   return result;
}
int main() {
   std::string binaryString = "01001";
   int flips = 4;
   std::cout << "Input binary string: " << binaryString << std::endl;
   std::cout << "Number of flips: " << flips << std::endl;
   std::string result = flipZeroes(binaryString, flips);
   std::cout << "Flipped string: " << result << std::endl;
   return 0;
}

輸出

Input binary string: 01001
Number of flips: 4
Flipped string: 00011
public class BinaryStringFlip {
   // Function to flip '0's in a binary string
   public static String flipZeroes(String binaryString, int flips) {
      char[] result = binaryString.toCharArray();
      int n = binaryString.length();
        
      int i = 0;
      while (i < n && flips > 0) {
         if (result[i] == '0') {
            result[i] = '1';
                
            if (i - 1 >= 0 && result[i - 1] == '1') {
               result[i - 1] = '0';
            } else if (i + 1 < n && result[i + 1] == '1') {
               result[i + 1] = '0';
            }
                
            flips--;
         }
         i++;
      }
        
      return new String(result);
   }

   public static void main(String[] args) {
      String binaryString = "01001";
      int flips = 4;
      System.out.println("Input binary string: " + binaryString);
      System.out.println("Number of flips: " + flips);
      String result = flipZeroes(binaryString, flips);
      System.out.println("Flipped string: " + result);
   }
}

輸出

Input binary string: 01001
Number of flips: 4
Flipped string: 00011
def flip_zeroes(binary_string, flips):
   result = list(binary_string)
   n = len(binary_string)
    
   i = 0
   while i < n and flips > 0:
      if result[i] == '0':
         result[i] = '1'
            
         if i - 1 >= 0 and result[i - 1] == '1':
            result[i - 1] = '0'
         elif i + 1 < n and result[i + 1] == '1':
            result[i + 1] = '0'
            
         flips -= 1
      i += 1
    
   return ''.join(result)

binary_string = "01001"
flips = 4
print("Input binary string:", binary_string)
print("Number of flips:", flips)
result = flip_zeroes(binary_string, flips)
print("Flipped string:", result)

輸出

Input binary string: 01001
Number of flips: 4
Flipped string: 00011

注意 − 對於不同的輸入字串和 K 值,輸出可能不同。

結論

總而言之,使用字串操作的強大功能可以有效地解決在考慮相鄰字元的情況下將二進位制字串中的 0 翻轉 K 次的問題。透過遵循提供的邏輯並迭代字串,我們可以根據其相鄰字元識別需要翻轉的 0。本教程中討論的實現展示了一種清晰簡潔的方法來解決這個問題,提供了一種可應用於現實世界場景的實用解決方案。無論是資料處理、演算法問題解決還是其他相關任務,能夠有效地透過翻轉具有不同鄰居的 0 來修改二進位制字串都是一項寶貴的技能。透過理解問題陳述並利用各種程式語言的功能,讀者可以自信地應對類似的挑戰並擴充套件其程式設計專業知識。

更新於:2023年10月20日

147 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.