《蓝桥杯算法》滑动窗口问题

《蓝桥杯算法》滑动窗口问题


一、最长不重复子序列

在这里插入图片描述

源代码:

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

string s;
int l, r;
int match;
map<char, int > window;
int ans = 1 << 31;

int main()
{
	cin >> s;
	while(r < s.length()){
		window[s[r]]++;
		if(window[s[r]] == 1){
			match++;
		}
		r++;
		while(r - l != match){
			if(s[l] != s[r]){
				match--;
			}
			window[s[l]]--;
			l++;
		}
		ans = max(ans, r - l);
	}
	cout << ans << endl;
	return 0;
}


/*
abcabcbb

bbbbb

pwwkew

*/

二、字符串匹配

在这里插入图片描述

源代码:

#include <iostream>
#include <map>
#include <vector>
using namespace std;

string s, t;     //s是开始串, t为目标串
int l, r;        //双指针
vector<int > ans;
map<char, int > need;         //记录目标串的字母出现的个数
map<char, int > window;      //记录开始串的字母出现的个数
int match;

int main()
{
	cin >> s >> t;
	for(int i = 0; i < t.length(); i++){
		need[t[i]]++;
	}
	while( r < s.length() && l <= r){
		if(need.count(s[r])){
			window[s[r]]++;
			if(window[s[r]] == need[s[r]]){    //这个判断语句必须得有,有多个b
				match++;
			}
		}
		r++;
		while(match == t.size()){
			if(r - l == t.size()){
				ans.push_back(l);
			}
			if(need.count(s[l])){
				window[s[l]]--;
				if(window[s[l]] < need[s[l]]){
					match --;
				}
			}
			l++;
		}
	}
	for(int i = 0; i < ans.size(); i++){
		cout << ans[i] << " ";
	}
	cout << endl;
	return 0;
}
/*
cbaebabacd
abc
*/


/*
abab
ab
*/

三、字符串匹配2

在这里插入图片描述
源代码:

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

string s, t;  //存入开始串   和   目标串:字母一定各不相同
int start, mi = 1 << 30;    //start记录最终串的最左边,  mi是最终串的长度
string ans;
int match;                  //表示匹配的字母的个数
map<char, int > need;       //记录目标串的每个字符的个数
map<char, int > window;     //记录开始串的每个字符的个数    window在这里就是滑动窗口
int l, r;                   //双指针

int main()
{
	cin >> s >> t;
	if( t.length() == 0 && t.length() > s.length()){
		ans = "";
	}
	for(int i = 0; i < t.length(); i++){
		need[t[i]]++;
	}
	while(r < s.length() && l <= r){
		//先找出字母符合条件的
		if(need.count(s[r])){
			window[s[r]]++;
			if(window[s[r]] == need[s[r]]){    //这个判断语句也是必须要加的
				match++;
			}
		}
		r++;
		//满足条件的情况下,找出最优解 使l++;
		while(match == need.size()){
			if(r - l < mi){
				start = l;
				mi = r - l;
			}
			if(need.count(s[l])){
				if(window[s[l]] == need[s[l]]){    //这样才能做到匹配
					match -- ;
				}
				window[s[l]]--;
			}
			l++;
		}
	}
	ans = ((mi == 1 << 30) ? "" : s.substr(start, mi));
	cout << ans <<endl;
	
	return 0;
}
/*
ADOBECODEBANC
ABC

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
滑动窗口算法是一种用于解决一些查找满足一定条件的连续区间的性质的问题的思想或技巧。它可以将双层嵌套的循环问题转换为单层遍历的循环问题,从而降低时间复杂度。滑动窗口算法的步骤通常包括维护两个指针left和right,表示当前窗口的左右边界。通过移动右指针扩大窗口,直到窗口内的元素之和满足某个条件。然后,移动左指针缩小窗口,直到不能再缩小为止。在这个过程中,记录窗口的最小长度,并更新最小长度的值。最后返回最小长度。滑动窗口算法的优点包括时间复杂度较低、空间复杂度较低、简单易懂。然而,滑动窗口算法也有一些缺点,包括无法解决所有子串问题、可能存在重复计算和可能存在局限性。总的来说,滑动窗口算法是一种有效的解决特定类型问题的方法。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [滑动窗口算法精讲(Sliding Window Algorithm)](https://blog.csdn.net/qq_39559641/article/details/122793321)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [滑动窗口算法](https://blog.csdn.net/m0_63951142/article/details/130671127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coder_小庞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值