Topic Connection
http://acm.hdu.edu.cn/showproblem.php?pid=1880
Magic Spell Dictionary Description
One of Harry Potter's compulsory courses at the school of Witchcraft and Wizardry is learning spells. It is said that there are 100000 different enchantments in the Wizarding world, and it is difficult for Harry to remember it all, but in order to fight the enemy, he must be able to invoke any desired spell in a critical moment, so he needs your help.
Give you a spell dictionary. When Harry hears a spell, your program must tell him the function of the spell, and when Harry needs a function but does not know what spell to use, your program will find the appropriate spell for him. If the spell he wants is not in the dictionary, the output is "what?"
Input
First, list no more than 100,000 different spell entries in the dictionary, each in the following format:
[Magic Spell] corresponding function
where "enchantments" and "corresponding functions" are strings that do not exceed 20 and 80 in length, the string guarantees that the characters "[" and "]" are not included, and that there is only one space between the "]" and the subsequent strings. The last line of the dictionary ends with "@[email protected", which does not belong to the dictionary entry.
The line after the dictionary contains a positive integer n (<=1000) followed by n test cases. Each test case takes on one line, or "[enchantments]", or "corresponding function".
Output
The output of each test case is one line, the output enchantment corresponds to the function, or the function corresponding to the enchantments. If the spell is not in the dictionary, output "what?"
Sample Input
[Expelliarmus] The disarming charm
[Rictusempra] Send a jet of silver light to hit the enemy
[Tarantallegra] Control the movement of one ' s legs
[Serpensortia] Shoot a snake out of the end of one ' s wand
[Lumos] Light The Wand
[Obliviate] the memory charm
[Expecto Patronum] Send a Patronus to the Dementors
[Accio] The summoning charm
@[email protected]
4
[Lumos]
The Summoning Charm
[Arha]
Take Me to the sky
Sample Output
Light the Wand
Accio
What?
What?
Started with the MP, MLE and then wrote a hash too bad mask (*/ω╲*).
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <map>using std::abs;using std::sort;using std::p air;using std::swap; Using std::vector;using std::multimap; #define PB (E) push_back (e) #define SZ (c) (int) (c). Size () #define MP (A, b) make_pair (A, B) #define ALL (c) (c). Begin (), (c). End () #define ITER (c) Decltype ((c). Begin ()) #define CLS (arr, Val) memset (arr, Val, si Zeof (arr)) #define Cpresent (C, E) (Find (All (c), (e))! = (c). End ()) #define REP (i, n) for (int i = 0; i < (int) n; i++) #defi NE tr (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) const int N = 100077;const int INF = 0x3f3f3f3f;typedef Unsig Ned Long Long ull;struct hash_set {struct Edge {char str1[22], str2[82]; int next;} G_1[n], g_2[n]; int tot_1, tot_2, Head_1[n], head_2[n]; inline void init () {tot_1 = 0, CLS (head_1,-1); tot_2 = 0, CLS (head_2,-1); } inline int Hash_func (char *s) {int VAL = 0; while (*s! = ') ') {val = ((val << 1) + (*s-' a '))% N; s++; } return Val; } inline void insert_1 (char *p, char *p1) {char *s = p; int val = Hash_func (p); int u = ABS (val)% N; strcpy (G_1[TOT_1].STR1, p); strcpy (G_1[TOT_1].STR2, p1); G_1[tot_1].next = Head_1[u], head_1[u] = tot_1++; } inline void Insert_2 (char *p, char *p1) {char *s = p; int val = Hash_func (p); int u = ABS (val)% N; strcpy (G_2[TOT_2].STR2, p); strcpy (G_2[TOT_2].STR1, p1); G_2[tot_2].next = Head_2[u], head_2[u] = tot_2++; } Inline Char *find_1 (char *p) {char *s = p; int val = Hash_func (p); int u = ABS (val)% N; for (int i = head_1[u]; ~i; i = g_1[i].next) {if (!strcmp (G_1[I].STR1, p)) return G_1[I].STR2; } return NULL; } Inline Char *find_2 (char *p) {char *s = p; int val = Hash_func (p); int U= ABS (val)% N; for (int i = head_2[u]; ~i; i = g_2[i].next) {if (!strcmp (G_2[I].STR2, p)) return G_2[I].STR1; } return NULL; }}hash;int Main () {#ifdef LOCAL freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w+", stdout); #endif Char *p; int n, K; Char buf[200], str1[100], str2[100]; Hash.init (); while (gets (BUF)) {if (!strcmp (buf, "@[email protected]") is break; CLS (str2, 0); p = strchr (buf, '] '); K = P-buf; strncpy (str1, buf + 1, k-1), str1[k-1] = ' + '; strcpy (STR2, p + 2); Hash.insert_1 (str1, str2), Hash.insert_2 (str2, str1); } scanf ("%d", &n); GetChar (); while (n--) {gets (BUF); if (buf[0] = = ' [') {p = strchr (buf, '] '); K = P-buf; strncpy (str1, buf + 1, k-1), str1[k-1] = ' + '; p = hash.find_1 (STR1); Puts (!p? "What?": P); } else {p = hash.find_2 (BUF); Puts (!p? "What?": P); }} return 0;}
Hdu 1880 Spell Dictionary