Hash and dichotomy

Posted by Donnamabob on Tue, 13 Aug 2019 10:46:26 +0200

Hash

What is Hash

Hash is generally used in the form of hash tables. Hash tables are a key-indexed structure for storing data. We can find the corresponding value by entering the value to be searched, that is, key.

What's the use of hashing?

Hash is a very efficient data structure. The biggest advantage of hashing is that it can quickly query whether a number exists in an array. If hashing conflicts are handled properly, each query can be completed in a very small constant.

Conflict management methods

1. Linear detection method

When a conflict occurs, look at the next unit in the table sequentially until an empty unit is found or the full table is searched.

2. Square detection method

When the conflict occurs, it is more flexible to carry out jump detection on the left and right of the table.

Two points

As a common search method, dichotomy search method improves the original linear time to logarithmic time, greatly shortens the search time, but it has a premise that it must be searched in ordered data.
Time complexity: O (n) --> O (logn)

Description
Give you a sequence, and then give you m elements, so that you can find out from the sequence the nearest digital output to each element, if there are two, output two.
Input
Multiple input groups, the first line gives you two numbers n (0 < n < 10000000), m (0 < m < n), followed by the number of n columns, and then input m elements, so that you can find the value closest to each element. If there are two, output from small to large.
Output
The m numbers output the values closest to each element separately, and an empty line is output between groups.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

const int N = 1e7+10;

int n;
int a[N];

void solve(int key)
{
    int l = 0, r = n-1;
    while(l <= r)
    {
        int mid = (l+r)/2;
        if(a[mid] == key)
        {
            printf("%d\n", a[mid]);
            return ;
        }
        else
        {
            if(key > a[mid]) l = mid+1;
            else r = mid-1;
        }
    }
    if(r == -1 || l == n)
    {
        if(r == -1) printf("%d\n", a[0]);
        else printf("%d\n", a[n-1]);
    }
    else
    {
        if(key-a[r] == a[l]-key) printf("%d %d\n", a[r], a[l]);
        else if(key-a[r] > a[l]-key) printf("%d\n", a[l]);
        else printf("%d\n", a[r]);
    }
}

int main()
{
    int m, x;
    while(~scanf("%d %d", &n, &m))
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
        }
        sort(a, a+n);
        while(m--)
        {
            scanf("%d", &x);
            solve(x);
        }
        printf("\n");
    }
    return 0;
}

Search for Data Structure Experiments VII: Linear Hash Table

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

const int N = 1510;

int n;
int Hash[N];

int main()
{
    int p;
    int x;
    while(~scanf("%d %d", &n, &p))
    {
        memset(Hash, -1, sizeof(Hash));
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &x);
            int tmp = x%p;
            if(Hash[tmp] == -1)
            {
                printf("%d", tmp);
                Hash[tmp] = x;
            }
            else
            {
                int flag = 0;
                for(int j = 0; j < p; j++)
                {
                    if(Hash[j] == x)
                    {
                        printf("%d", j);
                        flag = 1;
                        break;
                    }
                }
                if(!flag)
                {
                    while(Hash[tmp%p] != -1) tmp++;
                    Hash[tmp%p] = x;
                    printf("%d", tmp%p);
                }
            }
            if(i == n-1) printf("\n");
            else printf(" ");
        }
    }
    return 0;
}

Search for Data Structure Experiments Five: A Square Hash Table

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;

const int N = 2010;

int n;
int Hash[N];

int main()
{
    int p, x;
    while(~scanf("%d %d", &n, &p))
    {
        memset(Hash, -1, sizeof(Hash));
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &x);
            int tmp = x%p;
            if(Hash[tmp] == -1)
            {
                printf("%d", tmp);
                Hash[tmp] = x;
            }
            else
            {
                for(int j = 1; j <= p-1; j++)
                {
                    if(Hash[(x+j*j)%p] == -1)
                    {
                        printf("%d", (x+j*j)%p);
                        Hash[(x+j*j)%p]++;
                        break;
                    }
                    else if(Hash[(x-j*j)%p] == -1)
                    {
                        printf("%d", (x-j*j)%p);
                        Hash[(x-j*j)%p]++;
                        break;
                    }
                }
            }
            if(i == n-1) printf("\n");
            else printf(" ");
        }
    }
    return 0;
}

Dichotomy exercise
Search for Data Structure Experiments IV: Binary Search