zoukankan      html  css  js  c++  java
  • 第三课 双指针

     双指针算法:

    归并、快排等都是。

    双指针算法的作用,将O(n^2)的算法优化成O(n)的算法。

     输出一行字符串的每个单词:

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        string str;
        getline(cin,str);
        int n = (int)str.size();
        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;
            i = j;
        }
        return 0;
    }

    最长连续不重复数字的个数:

    #include <iostream>
    #include <cstring>
    using namespace std;
    const int N = 100010;
    int a[N],s[N];
    int main()
    {
        int n;
        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;
    }

     单链表:

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <cstring>
    using namespace std;
    const int N = 100010;
    int idx,e[N],ne[N],head;
    //initiation
    void init()
    {
        head = -1;
        idx = 0;
    }
    //insert to head node
    void insert(int x)
    {
        e[idx] = x;
        ne[idx] = head;
        head = idx;
        idx++;
    }
    //insert to the Kth node back
    void add(int k, int x)
    {
        e[idx] = x;
        ne[idx] = ne[k];
        ne[k] = idx;
        idx++;
    }
    //delete the Kth next node
    void del(int k)
    {
        ne[k] = ne[ne[k]];
    }
    
    int main()
    {
        int m;
        cin>>m;
        char a;int b,c;
        init();
        while (m--) {
            cin>>a>>b;
            if(a == 'H'){
                insert(b);
            }else if(a == 'D'){
                if(!b) head = ne[head];
                else del(b-1);
            }else if(a == 'I')
            {
                cin>>c;
                add(b-1,c);
            }
        }
        for(int i = head;i!=-1;i=ne[i])
            cout<<e[i]<<" ";
    }

     单调递增的两个数组的找出两个元素恰好等于一个给定的数:

    #include <cstdio>
    using namespace std;
    const int N = 100010;
    int a[N],b[N];
    int main()
    {
        int n,m,x;
        scanf("%d%d%d",&n,&m,&x);
        for(int i = 0;i<n;i++) scanf("%d",&a[i]);
        for(int i = 0;i<m;i++) scanf("%d",&b[i]);
        for(int i = 0;i<n;i++)
        {
            int j = m - 1;
            while(j >= 0 && a[i] + b[j] > x) j--;
            if(a[i] + b[j] == x) 
            {
                printf("%d %d
    ",i,j);
                return 0;
            }
        }
    }

     lowbit操作:计算每个数二进制中1的个数:

    #include <iostream>
    #include <cstring>
    using namespace std;
    const int N = 100010;
    int a[N];
    int lowbit(int n)
    {
        return n & (-n);
    }
    int main()
    {
        int n;
        cin>>n;
        for(int i = 0;i<n;i++)
        {
            cin>>a[i];
            int t = 0;
            while(a[i] & lowbit(a[i]))
            {
                t++;
                a[i] -= lowbit(a[i]);
            }
            cout<<t<<" ";
        }
        
    }
     
  • 相关阅读:
    hdu 5119 Happy Matt Friends
    hdu 5128 The E-pang Palace
    hdu 5131 Song Jiang's rank list
    hdu 5135 Little Zu Chongzhi's Triangles
    hdu 5137 How Many Maos Does the Guanxi Worth
    hdu 5122 K.Bro Sorting
    Human Gene Functions
    Palindrome(最长公共子序列)
    A Simple problem
    Alignment ( 最长上升(下降)子序列 )
  • 原文地址:https://www.cnblogs.com/longxue1991/p/12466000.html
Copyright © 2011-2022 走看看