Basic Algorithms (3)

Table of contents

1. Double pointer algorithm

2. Bit operations

3. Interval Merging


1. Double pointer algorithm

Double pointer arithmetic template:

for(int i = 0,j = 0;i < n;i++)
{
     while(j < i && check(i,j)) j++;
     //每道题的具体逻辑
}
  • 1.1 Two pointers point to two queues
  • 1.2 Two pointers point to a queue

Case exercises: 

split string

#include<iostream>
using namespace std;

int main()
{
    char str[1000];
    
    gets(str); //gets会读取空格,而不读取回车
    
    int n = strlen(str);
    
    for(int i = 0;i < n;i++){
        int j = i;
        while(j < n && str[j] != ' ') j++;
        
        //这道题的具体逻辑
        for(int k  =i;k < j;k++){
            cout<<str[k];
        }
        cout<<endl;
    }
    return 0;
}


Maximum non-repeating subsequence:

#include<iostream>
using namespace std;

const int N = 100010;

int a[N];
int s[N];

int main()
{
    cin>>n;
    for(int  i= 0;i < n;i++) cin>>a[i];
    
    int res = 0;
    for(int i = 0,j = 0;i < n;i++)
    {
        s[a[i]]++;
        while(s[a[i]] > 1)
        {
            s[a[j]] --;
            j ++;
        }
        res = max(res,i - j + 1);
    }
    cout<<res<<endl;
    return 0;
    
}

2. Bit operations

Common operations:

What is the kth digit in the binary representation of n (n>>k&1)

1. First shift the kth bit to the last bit (n >> k)

2. Look at the number of units (n &1)

#include<iostream>
using namespace std;

int main()
{
    int n = 10;
    for(int  k = 3;k>=0;k--)
    {
        cout<<(n >> k & 1)<<endl;
    }
    return 0;
    
}

lowbit(x): returns the last bit 1 of x

Example: x=1010, lowbit(x) returns 10

x=101000, lowbit(x) returns 1000

The basic application of lowbit(x), find the number of 1 in the binary number x

#include<iostream>
using namespace std;

int lowbit(int x)
{
    return x & -x;
}

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int x;
        cin>>x;
        
        int res = 0;
        while(x) x -= lowbit(x),res ++;  //每次减去x的最后一位1
        
        cout<<res<<endl;
    }
    return 0;
    
}

3. Interval Merging

Merge all intersecting intervals

step:

1. Sort by the left endpoint of the interval

2. Scan the entire interval, and check the relationship with other intervals during the scanning process

 3. Combine intervals

Case exercise code:

#include<iostream>
#include<algorithm>
#include<vector> //用vector来存储区间

using namespace std;

const int N = 100010;

typedef pair<int,int> PII;

int n;
vector<PII> segs;


void merge(vector<PII> &segs)
{
    vector<PII> res;
    
    
    //把所有区间排序
    sort(segs.begin(),segs.end());
    
    int st = -2e9,ed = -2e9;
    
    for(auto seg: segs)
    {
        if(ed < seg.first)
        {
            if(st != -2e9) res.push_back({st,ed});
            st = seg.first,ed = seg.second;
        }
        else
        {
            ed = max(ed,seg.second);
        }
    }
    if(st != -2e9) res.push_back({st,ed});
    
    segs = res;
}

int main()
{
    cin>> n;
    for(int i =0;i<n;i++)
    {
        int l,r;
        cin>>l>>r;
        segs.push_back({l,r});
    }
    
    merge(segs);
    
    cout<<segs.size()<<endl;
    
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_64443786/article/details/131881482