[Aizu] ITP2_8_A: Map: Search

topic

Portal: ITP2_8_A: the Map: Search

description

Dictionary for storing the M element formed by a key string and integer values, perform the operation given below, note that, in each of the M key must be unique

  • \ (INSERT (Key, x) \) : Insert a key element and a value of M to if already contains this key element, that element replacement is x
  • (GET (Key) \) \ : outputting a value corresponding to the specified key

Entry

Input formats are given in the following

\(q\)
\(query_{1}\)
\(query_{2}\)
\(.\)
\(.\)
\(query_{n}\)

Each \ (query_ {i} \) are:
\ (0 \) \ (Key \) \ (X \)
or
\ (1 \) \ (Key \)
where the first number 0 and 1 represent insert and get operations.

Export

For each get operation, the output line of an integer

limitation factor

  • \ (1 \ leq q \ leq 200000 \)
  • \ (1 \ leq x \ leq 1000000000 \)
  • \(1 \leq\) length of \(key \leq 20\)
  • \ (key \) all lowercase letters
  • For each get operation, comprising a given key elements must exist in the M.

Sample input

7
0 blue 4
0 red 1
0 white 5
1 red
1 blue
0 black 8
1 black

Sample Output

1
4
8

Solving

To achieve their own

analysis

Classic exercises on the hash table, not much explanation

design

Since the request does not exceed, 200,000, a hash table in order to make more efficient, space allocation, it is about twice
to create a structure for storing key-value pairs Ele elements constituting the
design of a hash function to calculate the corresponding string values
for simplicity, the insert and write directly get two general functions that operate directly on the array of global variables, are easily moved to the inside of the figures, all are poly fear in front, behind vacant, through the lucky

coding

#include <iostream>
#include <string>
using namespace std;
#define MAX_Q 400005

struct Ele {
    string key;
    int value;
    Ele() {
        key = "";
        value = 0;
    }
    Ele(string str, int i) {
        key = str;
        value = i;
    }
};

Ele space[MAX_Q];

long get_key(string str) {
    long total = 0;
    for (int i = 0; i < str.length(); i++) {
        total = (total * 4 + str[i] - 'a' + 1) % 1000000007;
    }
    return total;
}

void insert(string key, int value) {
    int index = get_key(key) % MAX_Q;
    while (space[index].key != "" && space[index].key != key) {
        index = (index + 17541) % MAX_Q;
    }
    Ele m(key, value);
    space[index] = m;
}

int search(string key) {
    int index = get_key(key) % MAX_Q;
    while (space[index].key != "" && space[index].key != key) {
        index = (index + 17541) % MAX_Q;
    }
    if (space[index].key == key) {
        return space[index].value;
    } else {
        return 0;
    }
} 

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int q;
    cin >> q;
    int com, value;
    string key;
    while (q--) {
        cin >> com >> key;
        if (com == 0) {
            cin >> value;
            insert(key, value);
        } else {
            cout << search(key) << endl;
        }
    }
}

result

to sum up

Prior to the wrong four times
for the first time: moderation is unclear, did not see the value corresponding to said key needs to be updated, direct throw inside, leading to get to the wrong element
Second: pointer only go backwards, forget the need to die array size, and error
third: for some long string, beyond the calculated hash value is outside the range of the int represents the
fourth: Although the modified calculated hash value, so that the output is long, but which has set up temporary values become int, leading to a temporary value prior to the modulo had not had time to overflow

STL

coding

#include <bits/stdc++.h>
using namespace std;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int q, com, x;
    string key;
    map<string, int> my_map;

    cin >> q;
    while (q--) {
        cin >> com >> key;
        if (com == 0) {
            cin >> x;
            my_map[key] = x;
        } else {
            cout << my_map[key] << endl;
        }
    }
}

result

Remark

Hey, I actually own time and space can be better than a template Diudiu, inexplicably happy

Guess you like

Origin www.cnblogs.com/by-sknight/p/10980859.html