hdu 1880 魔咒词典 (字符串hash)

题目链接

题意:给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”

思路:hash裸题。。。然而怎么感觉是第一次写hash呢。。。。

/* ***********************************************
Author :111qqz
Created Time :2016年11月20日 星期日 10时27分05秒
File Name :code/hdu/1880.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=1E5+7;
 7char magic[N][25],fun[N][81];
 8int cnt;
 9int n;
10map<int,int>mp1,mp2;
11unsigned int BKDRHash(char *str)
12{
13    unsigned int seed = 131;
14    unsigned int hash = 0 ;
15    while (*str) hash = hash*seed+(*str++);
16    return (hash&0x7fffffff);
17}
18int main()
19{
20	#ifndef  ONLINE_JUDGE 
21	freopen("code/in.txt","r",stdin);
22  #endif
23	cnt = 1 ;
24	while (~scanf("%s",magic[cnt]))
25	{
26	    if (strcmp(magic[cnt],"@END@")==0) break;
27	    getchar();
28	    gets(fun[cnt]);
29	    cnt++;
30	}
31	cnt--;
32	for ( int i = 1; i <= cnt ; i++)
33	{
34	    mp1[BKDRHash(magic[i])] = i ;
35	    mp2[BKDRHash(fun[i])] =  i;
36	}
37	scanf("%d",&n);
38	getchar();
39	char tmp[85];
40	while (n--)
41	{   
42	    gets(tmp);
43	    if (tmp[0]=='[')
44	    {
 1		if (!mp1[BKDRHash(tmp)])
 2		{
 3		    puts("what?");
 4		}
 5		else
 6		{
 7		    int id = mp1[BKDRHash(tmp)];
 8		    printf("%s",fun[id]);
 9		    printf("\n");
10		}
11	    }
12	    else
13	    {
14		if (!mp2[BKDRHash(tmp)])
15		{
16		    puts("what?");
17		}
18		else
19		{
20		    int id = mp2[BKDRHash(tmp)];
21		    int len = strlen(magic[id]);
22		    for ( int i = 1 ; i <len -1 ; i++) printf("%c",magic[id][i]);
23		    printf("\n");
1		}
2	    }
3	    ms(tmp,0);
4	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}