zoukankan      html  css  js  c++  java
  • HDU 1880 Hash table with chaining

    HDU 1880

    #include <bits/stdc++.h>
    #define DBG(x) cerr << #x << " = " << x << endl
    
    using namespace std;
    typedef long long LL;
    
    const unsigned int KEY = 137;
    const int BUCKET_NUM = 120000;
    
    struct Hash {
        vector<pair<string, string>> h[BUCKET_NUM];
    
        unsigned int get_hash(const string &s) {
            unsigned int hs = 0;
            for (auto c : s)
                hs = (hs * KEY + c) % BUCKET_NUM;
            return hs;
        }
    
        void insert(const string &k, const string &v) {
            unsigned int hs = get_hash(k);
            h[hs].push_back(make_pair(k, v));
        }
    
        string *get(const string &key) {
            unsigned int hs = get_hash(key);
            for (auto &kv : h[hs]) {
                if (kv.first == key)
                    return &kv.second;
            }
            return nullptr;
        }
    } key2val, val2key;
    
    pair<string, string> get_kv(const string &str) {
        unsigned long pos = 0;
        for (int i = 0; i < str.size(); i++) {
            if (str[i] == ']') {
                pos = i;
                break;
            }
        }
        auto k = str.substr(1, pos - 1);
        auto v = str.substr(pos + 2, str.size() - pos - 2);
        return make_pair(k, v);
    }
    
    int main(int argc, char **argv) {
        ios::sync_with_stdio(false);
        string s;
        while (true) {
            getline(cin, s);
            if (s == "@END@") break;
            auto kv = get_kv(s);
            key2val.insert(kv.first, kv.second);
            val2key.insert(kv.second, kv.first);
        }
        int n;
        cin >> n;
        cin.get();
        for (int i = 0; i < n; i++) {
            getline(cin, s);
            if (s[0] == '[') {
                auto val = key2val.get(s.substr(1, s.size() - 2));
                if (val == nullptr) {
                    cout << "what?" << "
    ";
                } else {
                    cout << *val << "
    ";
                }
            } else {
                auto key = val2key.get(s);
                if (key == nullptr) {
                    cout << "what?" << "
    ";
                } else {
                    cout << *key << "
    ";
                }
            }
        }
        return 0;
    }
    
    /**
    [expelliarmus] the disarming charm
    [rictusempra] send a jet of silver light to hit the enemy
    [tarantallegra] control the movement of one's legs
    [serpensortia] shoot a snake out of the end of one's wand
    [lumos] light the wand
    [obliviate] the memory charm
    [expecto patronum] send a Patronus to the dementors
    [accio] the summoning charm
    @END@
    4
    [lumos]
    the summoning charm
    [arha]
    take me to the sky
    
    light the wand
    accio
    what?
    what?
    */
    
  • 相关阅读:
    039 RabbitMq及数据同步01
    038 商品详情02-----页面静态化
    037 商品详情01
    036 搭建搜索微服务04----分类和品牌的过滤
    035 搭建搜索微服务03----页面分页效果
    034 通过域名访问服务器或本地的图片资源---switchhost+nginx
    033 搭建搜索微服务02----实现基本搜索功能
    ubuntu server 无线网卡的处理
    grub 启动错误 "file not found"
    Ubuntu 的 desktop 和 server 还是有区别。
  • 原文地址:https://www.cnblogs.com/ToRapture/p/11904084.html
Copyright © 2011-2022 走看看