The seventh chapter of the data structure search job (corrected)

True or False

1-1
Store M elements in a hash table represented by an array of length S, then the filling factor of the table is M/S.

T F

Textbook P226: The filling factor α of the hash table is defined as = the number of records filled in the table / the length of the hash table;

1-2
Even if two elements are hashed into a table with 100 units, there may still be conflicts.

T F

This question mainly investigates that the hash function can be changed. The choice of conflict is based on the choice of the function;
Textbook P223: Choosing a "good" hash function can reduce conflicts to a certain extent, but in practical applications, It is difficult to completely avoid conflicts, so choosing an effective method to handle conflicts is another key issue of hashing;

1-3
Hash 10 elements into a hash table of 100 000 units, and there will be no conflicts.

T F

Due to the selection of the hash function, address conflicts may still occur, which cannot be absolutely avoided; the
same as 1-2;

Multiple choice

2-1
Given a sequence table L with a length of 16, the elements are arranged in order by keywords. If the binary search method is used to find an element that does not exist in L, the maximum number of keyword comparisons is:

A. 4
B. 5
C. 6
D. 7

The maximum number of comparisons for binary search is
Insert picture description here
log(16) + 1 = 4 + 1 = 5;

2-2
Use binary search to find a certain number from 100 ordered integers. In the worst case, the number of comparisons required is:

A. 7
B. 10
C. 50
D. 99

The maximum number of comparisons for binary search is
Insert picture description here
2 ^ 6 = 64;
log(100) + 1 = 6 + 1 = 7;

2-3
Among the following search methods, the search method whose average search length has nothing to do with the number of nodes is:

A. Search in order
B. Dichotomy
C. Use hash (hash) tables
D. Use binary search tree

The characteristic of the hash table search method is that the average search length has nothing to do with the number of nodes n;

2-4 If
10 elements are hashed into a hash table of 100,000 units, is there a conflict?

A. Certainly
B. Maybe
C. Certainly not
D. One in ten thousand chance

2-5
Suppose the address range of the hash table is [0,16], and the hash function is H(Key)=Key%17. The linear detection method is used to deal with the conflict, and the key sequence {26, 25, 72, 38, 8, 18, 59} is sequentially stored in the hash table. The address of element 59 stored in the hash table is:

A. 8
B. 9
C. 10
D. 11

Textbook P223: Linear Detection Method: This detection method can imagine the hash table as a circular table. When a conflict occurs, the empty cell is searched from the next cell of the conflicting address. If no empty cell is found to the last position, then return Continue to search at the head of the table until you find a space, then put this element into this space. If no space is found, the hash table is full and overflow processing is required.
Process:
26% 17 = 9, so address 9 stores 26;
25% 17 = 8, so address 8 stores 25;
72% 17 = 4, so address 4 stores 72;
38% 17 = 4, because address 4 conflicts, go Move one unit down, so address 5 stores 38;
8% 17 = 8, because address 8 conflicts, move down one unit, and because address 9 conflicts, move down one unit, so address 10 stores 8;
18% 17 = 1, so address 1 stores 18;
59% 17 = 8, because address 8 conflicts, move down one unit, and because address 9 conflicts, move down one unit, and because address 10 conflicts, move down one unit, So address 11 stores 59;

2-6
When looking up a node whose value is equal to X from a singly linked list with N nodes, how many nodes need to be compared on average if the search is successful?

A. N/2
B. N
C. (N−1)/2
D. (N+1)/2

Find a node whose value is equal to x from a singly linked list with n nodes. If the search is successful, (n+1)/2 nodes need to be compared on average.
Since the singly linked list can only be searched in a one-way order, taking the search from the first node as an example, the number of nodes that need to be compared to find the m-th node f(m)=m, the best case for a successful search is the first time If the search is successful, only 1 node is compared. In the worst case, the search is successful at the end, and n nodes need to be compared.
So there are a total of n cases, and on average, the nodes to be compared are (1+2+3+...+(n-1)+n)/n=(n+1)/2.
Textbook P193;

2-7
Use a binary search for a sorted table of length 10. If the search is unsuccessful, at least the number of comparisons required is ().

A. 4
B. 3
C. 5
D. 6

Draw a binary sort tree, and it will come out after a comparison. Find the depth of the tree.
Insert picture description here
So at least how many times? ? doubt? ?

Programming questions

7-1 Airline VIP customer inquiry (25 points)

Many airlines will provide preferential membership services. When a customer’s mileage accumulates to a certain amount, the mileage points can be used to directly redeem award tickets or award upgrades. Given the flight records of all members of an airline, it is required to realize the function of quickly querying member mileage points based on the ID number.

Input format: The
input first gives two positive integers N (≤10^​5) and K (≤500). Among them, K is the minimum mileage, that is, to take care of members who take short-haul flights, airlines will also accumulate flights with a range of less than K kilometers as K kilometers. Next N lines, each line gives a flight record. The input format of the flight record is: 18-digit ID number (space) flight mileage. The ID number is composed of 17 digits and the last check code. The value range of the check code is 0-9 and 11 symbols x; the unit of flight mileage is kilometers, which is within the interval (0, 15 000) An integer. Then give a positive integer M (≤10^​5), and then give the ID number of the inquirer of M lines.

Output format:
For each query person, the current mileage accumulation value is given. If the person is not a member, No Info is output. Each query result occupies one row.

Input sample:

4 500
330106199010080419 499
110108198403100012 15000
120104195510156021 800
330106199010080419 1
4
120104195510156021
110108198403100012
330106199010080419
33010619901008041x

Sample output:

800
15000
1000
No Info
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
#define MAXN 10005
typedef long long LL;
/*
hash表
*/

typedef struct node
{
    
    
    char id[20];
    LL miles;
    struct node* next;
}*List;
typedef struct tb
{
    
    
    LL Tablesize;
    List *list;
}*Hashlist;
LL Hash(char key[],LL size)
{
    
    
    LL tmp = 0;
    for(LL i=13;i<18;i++)
    {
    
    
        if(key[i]=='x')
            tmp = (tmp*10+10)%size;
        else
            tmp = (tmp*10 + key[i]-'0')%size;
    }
    return tmp;
}
LL NextPrim(LL x)
{
    
    
    LL j;
    for(LL i=x;;i++)
    {
    
    
        for(j=2;j*j<=i;j++)
            if(i%j==0)
                break;
        if(j*j>i)
            return i;
    }
}
Hashlist Init(LL size)
{
    
    
    Hashlist H = (Hashlist)malloc(sizeof(tb));
    H->Tablesize = NextPrim(size);
    H->list = (List*)malloc(sizeof(List)*H->Tablesize);
    for(LL i=0;i<H->Tablesize;i++)
    {
    
    
        H->list[i] = (List)malloc(sizeof(node));
        H->list[i]->next = NULL;
    }
    return H;
}
List Find(char key[],Hashlist H)
{
    
    
    List t = H->list[Hash(key,H->Tablesize)];
    List p = t->next;
    while(p!=NULL && strcmp(key,p->id))
        p = p->next;
    return p;
}
void Insert(char key[],LL miles,Hashlist H)
{
    
    
    List t = H->list[Hash(key,H->Tablesize)];
    List f = Find(key,H);
    if(f==NULL)
    {
    
    
        List tmp = (List)malloc(sizeof(node));
        tmp->miles = miles;
        strcpy(tmp->id,key);
        tmp->next = t->next;
        t->next = tmp;
    }
    else
    {
    
    
        (f->miles) += miles;
    }
}

int main()
{
    
    
    char id[20];
    LL tmp,n,m,k;
    scanf("%lld%lld",&n,&k);
    Hashlist H = Init(n);
    for(LL i=0;i<n;i++)
    {
    
    
        scanf("%s%lld",id,&tmp);
        if(tmp<k) tmp = k;
        Insert(id,tmp,H);
    }
    scanf("%lld",&m);
    for(LL j=0;j<m;j++)
    {
    
    
        scanf("%s",id);
        List f = Find(id,H);
        if(f==NULL)
            printf("No Info\n");
        else
            printf("%lld\n",f->miles);
    }
}

Guess you like

Origin blog.csdn.net/Jessieeeeeee/article/details/107029762