zoukankan      html  css  js  c++  java
  • HDU 1880 简单Hash

    题目链接:【http://acm.hdu.edu.cn/showproblem.php?pid=1880】

    中文题面,题意很简单;

    题解: 把每个 魔咒 和 对应的功能分别Hash,然后分别映射到map<ULL,string>里面,(魔咒Hash值,对应的功能)和(对应功能Hash值,魔咒)。

    Hash 方式采用最简单的那种Hash即可。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 100 + 15;
    const int sed = 131;
    typedef unsigned long long ULL;
    unordered_map<ULL, string>mpa, mpb;
    char tmp[maxn], sa[maxn], sb[maxn];
    ULL get_Hash(char s[], int len)
    {
        ULL ret = 0;
        for(int i = 0; i < len; i++)
        {
            ret = ret * sed + s[i];
        }
        return ret;
    }
    int main ()
    {
        while(true)
        {
            gets(tmp);
            int len = strlen(tmp);
            if(!strcmp("@END@", tmp)) break;
            int cnt = 0, pos = 0;
            for(int i = 1; i < len; i++)
            {
                if(tmp[i] == ']')
                {
                    pos = i + 2;
                    break;
                }
                sa[cnt++] = tmp[i];
            }
            sa[cnt] = 0;
            ULL ta = get_Hash(sa, cnt);
            cnt = 0;
            for(int i = pos; i < len; i++)
                sb[cnt++] = tmp[i];
            sb[cnt] = 0;
            ULL tb = get_Hash(sb, cnt);
            mpa[ta] = (string)(sb);
            mpb[tb] = (string)(sa);
        }
        int N;
        scanf("%d", &N);
        gets(tmp);
        for(int i = 1; i <= N; i++)
        {
            gets(tmp);
            int len = strlen(tmp);
            if(tmp[0] == '[')
            {
                for(int i = 0; i < len; i++)
                    tmp[i] = tmp[i + 1];
                tmp[len - 2] = 0;
                ULL T = get_Hash(tmp, len - 2);
                if(mpa.count(T))
                    cout << mpa[T] << endl;
                else
                    cout << "what?" << endl;
                continue;
            }
            ULL T = get_Hash(tmp, len);
            if(mpb.count(T))
                cout << mpb[T] << endl;
            else
                cout << "what?" << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    NOIP2010提高组乌龟棋 -SilverN
    NOIP2009 提高组T3 机器翻译 解题报告-S.B.S
    controller向layout传值
    解决Yii2中刷新网页时验证码不刷新的问题
    关联表 重命名
    定义action的允许访问方式
    js 拷贝clone
    是否包含指定字符串
    indexOf()定义和用法
    直接借鉴的 ids拼接
  • 原文地址:https://www.cnblogs.com/pealicx/p/7577071.html
Copyright © 2011-2022 走看看