当前位置:   article > 正文

C++——滑动窗口_c++滑动窗口

c++滑动窗口

滑动窗口算法(Sliding Window Algorithm)是一种用于解决数组或字符串相关问题的常用技术。它通过维护一个固定大小的窗口,并在窗口内移动来处理数据。该算法通常用于在线性时间内解决连续子数组或子字符串的问题。

滑动窗口算法的基本思想是通过调整窗口的起始位置和结束位置,以有效地处理数据。算法的核心步骤如下:

1、初始化窗口的起始位置和结束位置,通常为数组或字符串的开头。

2、在每次迭代中,计算当前窗口的状态,并处理相关数据。

3、根据问题的要求,调整窗口的起始位置和结束位置,使窗口滑动到下一个位置。

4、重复步骤2和步骤3,直到窗口滑动到数据的末尾。

滑动窗口算法的关键是确定窗口的大小和如何移动窗口。这取决于具体的问题。下面是两个示例来说明滑动窗口算法的应用:

1、最大子数组和问题(Maximum Subarray Sum Problem):给定一个整数数组,找到该数组中和最大的连续子数组。滑动窗口算法可以通过保持一个窗口的和,将窗口向右移动一位,并根据需要调整窗口的大小来解决这个问题。

  1. #include <iostream>
  2. #include <vector>
  3. int maxSubarraySum(const std::vector<int>& nums, int k) {
  4. int n = nums.size();
  5. int windowSum = 0;
  6. int maxSum = INT_MIN;
  7. // 计算初始窗口的和
  8. for (int i = 0; i < k; ++i) {
  9. windowSum += nums[i];
  10. }
  11. // 滑动窗口
  12. for (int i = k; i < n; ++i) {
  13. windowSum += nums[i] - nums[i - k]; // 加上新元素,减去窗口的最左侧元素
  14. maxSum = std::max(maxSum, windowSum);
  15. }
  16. return maxSum;
  17. }
  18. int main() {
  19. std::vector<int> nums = {1, -3, 2, 4, -1, 5, -7, 2, 6};
  20. int k = 3; // 窗口大小
  21. int result = maxSubarraySum(nums, k);
  22. std::cout << "Maximum subarray sum: " << result << std::endl;
  23. return 0;
  24. }

运行上述代码将输出最小覆盖子串为"11"。

2、字符串最小覆盖子串问题(Minimum Window Substring Problem):给定一个源字符串和一个目标字符串,在源字符串中找到包含目标字符串所有字符的最短子串。滑动窗口算法可以通过维护两个指针,一个指向窗口的起始位置,一个指向窗口的结束位置,并根据目标字符串的出现情况来移动窗口,从而找到最小的覆盖子串。

  1. #include <iostream>
  2. #include <unordered_map>
  3. std::string minWindowSubstring(const std::string& source, const std::string& target) {
  4. std::unordered_map<char, int> targetMap;
  5. std::unordered_map<char, int> windowMap;
  6. int left = 0;
  7. int right = 0;
  8. int minLen = INT_MAX;
  9. int minStart = 0;
  10. int count = 0;
  11. // 统计目标字符串中每个字符的出现次数
  12. for (char ch : target) {
  13. ++targetMap[ch];
  14. }
  15. // 滑动窗口
  16. while (right < source.length()) {
  17. char ch = source[right];
  18. ++windowMap[ch];
  19. // 如果当前字符在目标字符串中,并且窗口中该字符的出现次数不超过目标字符串中该字符的出现次数,则count加1
  20. if (targetMap.count(ch) && windowMap[ch] <= targetMap[ch]) {
  21. ++count;
  22. }
  23. // 如果窗口包含目标字符串中的所有字符
  24. while (count == target.length()) {
  25. // 更新最小覆盖子串的起始位置和长度
  26. if (right - left + 1 < minLen) {
  27. minLen = right - left + 1;
  28. minStart = left;
  29. }
  30. // 缩小窗口,尝试去掉最左侧的字符
  31. char leftCh = source[left];
  32. --windowMap[leftCh];
  33. // 如果移除最左侧字符后,窗口不再包含目标字符串中的所有字符,则count减1
  34. if (targetMap.count(leftCh) && windowMap[leftCh] < targetMap[leftCh]) {
  35. --count;
  36. }
  37. ++left;
  38. }
  39. ++right;
  40. }
  41. if (minLen == INT_MAX) {
  42. return "";
  43. }
  44. return source.substr(minStart, minLen);
  45. }
  46. int main() {
  47. std::string source = "ADOBECODEBANC";
  48. std::string target = "ABC";
  49. std::string result = minWindowSubstring(source, target);
  50. std::cout << "Minimum window substring: " << result << std::endl;
  51. return 0;
  52. }

运行上述代码将输出最小覆盖子串为"BANC"。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/676696
推荐阅读
相关标签
  

闽ICP备14008679号