首頁 >後端開發 >C++ >最少需要替換的字元數,使得字串連結成一個長度為K的回文字串

最少需要替換的字元數,使得字串連結成一個長度為K的回文字串

WBOY
WBOY轉載
2023-08-30 09:37:06968瀏覽

最少需要替換的字元數,使得字串連結成一個長度為K的回文字串

追蹤最少的字元數量,以將給定的字串轉換為長度為K的回文子字串鏈接,是字串控制領域中的常見問題。讀取相同步驟並倒置的字串稱為回文字串。例如,"radar"或"level"。本文將涵蓋用於有效解決此問題的基本概念、方法和潛在的最佳化策略。透過本文的結論,讀者將能夠處理類似的字串操作問題,因為他們將全面了解所需步驟

問題將在接下來的段落中詳細解釋,然後將討論每種方法的優缺點。所選方法將進行徹底的檢查,並提供程式碼範例以展示如何使用它們。我們還將檢查每種方法的時間複雜度,以了解在不同的輸入數量下它們的有效性

使用的方法

  • Brute-Force方法

  • Sliding Window Approach

Brute-Force Approach

的中文翻譯為:

暴力破解方法

The Brute-Force The approach for finding the fewest characters to be supplanted to form a string concatenation of a K-length palindromic string includes checking all possible substrings of length Kwo within the string includes checking all possible substrings of length Kwo within the given string It ps pointers, cleared out and right, to the begin and conclusion of the K-character substring, initialize a variable to track the least substitutions, and iterate over the string, upgrading the window with the proper pointer moving one step right each each right moeach y right。 window, check in case it could be a palindrome by comparing characters from left and right, and tally the number of substitutions required on the off chance that it's not a palindrome. Keep track of the fewest place found the conclusion of the string. The result will be the fewest substitutions required to realize the specified K-length palindromic substring. In any case, this approach has high time complexity, making it wasteful for huge approach has high time complexity, making it wasteful for strings.

Algorithm

  • Consider each substring of length K as you iterate through the provided string.

  • 驗證每個子字串是否為回文

  • Count how many characters would need to be changed if it weren't already a palindrome.

  • 盡可能少保留需要替換的子字串

  • Make a palindrome by changing the characters in the minimal replacement substring.

Example

#include <iostream>
#include <string>
using namespace std;

string minimalReplacementPalindromeSubstring(const string& str, int K) {
   int n = str.length();
   string minReplacementSubstr;

   for (int i = 0; i <= n - K; i++) {
      string substr = str.substr(i, K);
      int replacements = 0;
      for (int j = 0; j < K / 2; j++) {
         if (substr[j] != substr[K - 1 - j]) {
            replacements++;
         }
      }

      if (replacements < minReplacementSubstr.length() || minReplacementSubstr.empty()) {
         minReplacementSubstr = substr;
      }
   }

   return minReplacementSubstr;
}

int main() {
   string input = "iurefhuhfrati";
   int K = 4;

   string result = minimalReplacementPalindromeSubstring(input, K);
   cout << "Minimal Replacement Substring: " << result << endl;

   return 0;
}

輸出

Minimal Replacement Substring: rati

滑動視窗方法

滑動視窗方法可以用於有效地解決問題,包括子數組或子字串操作。在尋找最少字元以建立長度為K的回文字串的字串連接的情況下,該方法包括在導航輸入字串時保持一個固定大小的視窗(子字串)為K個字符

計算設定了兩個指標'left'和'right',起初指示K個字元子字串的開始和結束。然後,它決定將此子字串轉換為回文所需的替換次數。為了追蹤所需的最少替換次數,初始化了一個變數'min_replacements'。

Algorithm

  • 設定兩個指針,left和right,分別指向主要的K個字元子字串的開頭和結尾

  • 決定預期將子字串轉換為回文的替換次數。

  • 為了追蹤所需的最低替換次數,初始化變數 min_replacements

  • 透過將右指標向右移動一個位置來更新視窗

  • 如果目前視窗是回文,則移動右指針

  • Calculate the amount of replacements required and, if necessary, change min_replacements if the current window is not a palindrome.

  • To update the window, move the left pointer one space to the right.

  • Up to the string's conclusion, repeat steps 4 through 7.

  • 子字串的字元應該用盡可能少的替換進行替換

Example

#include <iostream>
#include <string>
using namespace std;

int minReplacementsForPalindrome(string s, int k) {
   int left = 0, right = k - 1, min_replacements = 0;
   while (right < s.length()) {
      int i = left, j = right;
      while (i < j) {
         if (s[i] != s[j])
            min_replacements++;
         i++;
         j--;
      }
      left++;
      right++;
   }
   return min_replacements;
}

int main() {
   string input_string = "abcxyzuvw"; // Replace this with your desired input string
   int k = 3; // Replace this with the desired value of K
   int result = minReplacementsForPalindrome(input_string, k);
   cout << "Minimum replacements required: " << result << endl;
   return 0;
}

輸出

Minimum replacements required: 7

Conclusion

本文探討了將給定字串轉換為長度為K的回文子字串的最小字元數的問題。它研究了兩種基本方法來解決這個問題:暴力方法和滑動視窗方法。暴力方法包括檢查給定字串中所有可能的長度為K的子字串,判斷它們是否是回文,並檢查必要的替換。然而,這種方法的複雜度很高,對於大字串來說效率低下

另一方面,滑動視窗方法透過保持固定大小的視窗並在導航輸入字串時有效地更新視窗來優化此方法。本文提供了程式碼測試和經驗,以幫助使用者更成功地理解和解決類似的字串處理問題。

以上是最少需要替換的字元數,使得字串連結成一個長度為K的回文字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除