Summary of sliding window algorithm

Preface

I am Zihan who can write bugs. My life is endless and my algorithm never stops!
Last night, I had nothing to do and went to the school ACM team's training computer room for a while. I was so stimulated that the freshman pressed me on the ground and rubbed me. It was really exciting and I learned a sliding window algorithm. Alas, the Algorithm course has just started. I have studied it for a while before, but I rarely brushed up on it. From now on, I will often go to train with them at night. I would like to call this behavior "stealing". (manual funny)

Algorithmic thinking

I personally think that the idea of ​​the sliding window algorithm is similar to that of double pointers. It simulates a window, and the created window gradually slides. According to the constraints given by the question, the window boundary is updated, thereby continuously updating the value required by the question. When the window slides to the boundary , this also means that when the algorithm ends, the required value can be obtained.Generally speaking, the right border slides actively and the left border slides passively.

As the saying goes, practice is the only criterion for testing truth! Below, we will go through three sliding window algorithm questions to gain an in-depth understanding of the practical application of the sliding window algorithm.

sliding window I

sliding window I

Problem-solving ideas

By setting two pointers as the boundaries of the window, the sliding window is simulated. When the characters on the right boundary of the window already exist in the window, the left boundary needs to be updated. At the same time, the maximum substring length is updated. When the right pointer moves to the end of the string and the window slides, the resulting value is the maximum substring length value.

code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    string s;
    while(getline(cin,s))
    {
    
    
        int i=0,j=0,curlen=0,maxlen=0;
        if(s.length()==0)
        {
    
    
            cout<<0<<endl;
            return 0;
        }
        for(;j<s.length();j++)
        {
    
    
            curlen++;
            for(int k=i;k<=j;k++)//扫描有无重复字符
            {
    
    
                if(s[k]==s[j+1])//存在重复字符
                {
    
    
                    if(curlen>maxlen) maxlen=curlen;//更新值
                    i=k+1;//跳过重复字符
                    curlen=j-i+1;//更新值
                    break;
                }
            }
        }
        maxlen=curlen>maxlen?curlen:maxlen;//更新值
        cout<<maxlen<<endl;
    }
    return 0;
}

Sliding window II

Insert image description here

Problem-solving ideas

Set up two double pointers. The right pointer slides. When the value is greater than or equal to m, the value is updated. Then slide the left pointer to less than m. The right pointer continues to slide to the above situation for processing until the right pointer slides to the window. boundary, the algorithm ends.

code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    int Start=0,End=0,sum=0;
    int res=10000000;
    int n,m;
    cin>>n;
    int a[n];
    for(int i=0; i<n; i++)
        cin>>a[i];
    cin>>m;
    while(End<n)//右指针未到达边界
    {
    
    
        sum+=a[End];//右指针滑动
        if(sum>=m)//出现大于等于m的情况
        {
    
    
            res=min(res,End-Start+1);更新值
            while(sum>=m&&Start<=End)
            {
    
    
                res=min(res,End-Start+1);更新值
                sum-=a[Start];
                Start++;//左指针移动
            }
        }
        End++;//不满足情况,右指针继续移动
    }
    if(res==10000000)
        cout<<0<<endl;
    else
        cout<<res<<endl;
    return 0;
}

111111

Insert image description here

Problem-solving ideas

Set up double pointers. The right pointer slides. When it encounters 1, it continues to slide. When it encounters 0, it is judged whether the number of 0s in the window at this time is equal to the m given by the question. If it is equal, the left pointer moves until it is removed. to a 0 to make room for a new 0. If it is less than m, the right pointer continues to move. Then the value is updated until the right pointer moves to the window boundary, and the algorithm ends.

code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    int n,m,Max=0;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    cin>>m;
    int left=0,right=0;
    for(;right<n;right++)
    {
    
    
        if(a[right]==0)
        {
    
    
            if(m==0)//到达限制条件
            {
    
    
                while(a[left]==1)
                    left++;
                left++;//去除一个0
            }
            else
                m--;
        }
        Max=max(right-left+1,Max);//更新值
    }
    cout<<Max<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/pipihan21/article/details/114665832