码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1880 简单Hash

时间:2017-09-22 22:30:55      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:turn   简单的   scan   char   链接   namespace   c++   highlight   clu   

题目链接:【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;
}

  

HDU 1880 简单Hash

标签:turn   简单的   scan   char   链接   namespace   c++   highlight   clu   

原文地址:http://www.cnblogs.com/pealicx/p/7577071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!