hdu 1880 魔咒词典 (字符串hash)
题意:给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”
思路:hash裸题。。。然而怎么感觉是第一次写hash呢。。。。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年11月20日 星期日 10时27分05秒
4File Name :code/hdu/1880.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E5+7;
34char magic[N][25],fun[N][81];
35int cnt;
36int n;
37map<int,int>mp1,mp2;
38unsigned int BKDRHash(char *str)
39{
40 unsigned int seed = 131;
41 unsigned int hash = 0 ;
42 while (*str) hash = hash*seed+(*str++);
43 return (hash&0x7fffffff);
44}
45int main()
46{
47 #ifndef ONLINE_JUDGE
48 freopen("code/in.txt","r",stdin);
49 #endif
50 cnt = 1 ;
51 while (~scanf("%s",magic[cnt]))
52 {
53 if (strcmp(magic[cnt],"@END@")==0) break;
54 getchar();
55 gets(fun[cnt]);
56 cnt++;
57 }
58 cnt--;
59 for ( int i = 1; i <= cnt ; i++)
60 {
61 mp1[BKDRHash(magic[i])] = i ;
62 mp2[BKDRHash(fun[i])] = i;
63 }
64 scanf("%d",&n);
65 getchar();
66 char tmp[85];
67 while (n--)
68 {
69 gets(tmp);
70 if (tmp[0]=='[')
71 {
72
73 if (!mp1[BKDRHash(tmp)])
74 {
75 puts("what?");
76 }
77 else
78 {
79 int id = mp1[BKDRHash(tmp)];
80 printf("%s",fun[id]);
81 printf("\n");
82 }
83 }
84 else
85 {
86 if (!mp2[BKDRHash(tmp)])
87 {
88 puts("what?");
89 }
90 else
91 {
92 int id = mp2[BKDRHash(tmp)];
93 int len = strlen(magic[id]);
94 for ( int i = 1 ; i <len -1 ; i++) printf("%c",magic[id][i]);
95 printf("\n");
96
97 }
98 }
99 ms(tmp,0);
100 }
101
102
103
104 #ifndef ONLINE_JUDGE
105 fclose(stdin);
106 #endif
107 return 0;
108}