• 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;
    }
    

      

  • 相关阅读:
    放大图 带回弹效果
    用recyclerview实现对话(通过接口实现)
    java 问号表达式
    recyclerview的使用
    我遇到的报错信息整理
    横竖屏切换
    NOIP 2017提高组自测 奶酪
    小蝌蚪找妈妈 牛客
    牛客练习赛50 C tokitsukaze and Soldier
    洛谷P1630 求和
  • 原文地址:https://www.cnblogs.com/pealicx/p/7577071.html
Copyright © 2020-2023  润新知