魔咒词典 【HDU - 1880】【双值哈希】

题目链接

  这道题更多的感觉是在考你对于字符类型的处理,用了scanf("%s")不行,然后转用gets()然后就可以正常输入输出了,最后有一件很神奇的事,不要用G++,有毒:

同样的代码,我用了C++交就过了。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN=1e5+7;       //同时充当mod作用
const int Hash_1=131, Hash_2=233;
int Q;
map<pair<int, int>, string> mp;
char is[300];
char a[110], b[110];
void init()
{
    mp.clear();
}
int get1(char *s)
{
    int ans=0;
    for(int i=0; s[i]; i++) { ans=( ans*Hash_1%maxN + s[i] )%maxN; }
    return ans;
}
int get2(char *s)
{
    int ans=0;
    for(int i=0; s[i]; i++) { ans=( ans*Hash_2%maxN + s[i] )%maxN; }
    return ans;
}
int main()
{
    init();
    while(gets(is) && is[0]!='@')
    {
        int len=(int)strlen(is);
        int i=1, j=0, tmp1=0, tmp2=0, tmp3=0, tmp4=0;
        for(i=1; is[i]!=']'; i++) a[i-1]=is[i];
        a[i-1]=0;
        tmp1=get1(a);
        tmp2=get2(a);
        i+=2;
        for(j=i; j<len; j++) b[j-i]=is[j];
        b[j-i]=0;
        tmp3=get1(b);
        tmp4=get2(b);
        mp[make_pair(tmp1, tmp2)]=b;
        mp[make_pair(tmp3, tmp4)]=a;
        //getchar();
    }
    scanf("%d", &Q);
    getchar();
    while(Q--)
    {
        //scanf("%s", is);
        gets(is);
        if(is[0]=='[')
        {
            int len=(int)strlen(is);
            is[len-1]=0;
            int tmp1=0, tmp2=0;
            tmp1=get1(is+1);
            tmp2=get2(is+1);
            if(mp.find(make_pair(tmp1, tmp2))!=mp.end()) cout<<mp[make_pair(tmp1, tmp2)]<<endl;
            else printf("what?\n");
        }
        else
        {
            int len=(int)strlen(is);
            is[len]=0;
            int tmp1=0, tmp2=0;
            tmp1=get1(is);
            tmp2=get2(is);
            if(mp.find(make_pair(tmp1, tmp2))!=mp.end()) cout<<mp[make_pair(tmp1, tmp2)]<<endl;
            else printf("what?\n");
        }
        //getchar();
    }
    return 0;
}

 

get1()得到的是第一重哈希,get2()得到的是第二重哈希,我们用双重哈希来得到精确的哈希值答案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值