首页 >后端开发 >C++ >最少需要替换的字符数,使得字符串连结成一个长度为K的回文字符串

最少需要替换的字符数,使得字符串连结成一个长度为K的回文字符串

WBOY
WBOY转载
2023-08-30 09:37:06976浏览

最少需要替换的字符数,使得字符串连结成一个长度为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 K within the given string. It takes after the steps: set two 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 time. For each 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 replacements found so far. Proceed with this preparation until 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 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删除