單詞匹配 - hash
阿新 • • 發佈:2018-07-28
tar con bre open const oid res clas ons 哈利波特在魔法學校的必修課之一就是學習魔咒。據說魔法世界有100000種不同的魔咒,哈利很難全部記住,但是為了對抗強敵,他必須在危急時刻能夠調用任何一個需要的魔咒,所以他需要你的幫助。
給你一部魔咒詞典。當哈利聽到一個魔咒時,你的程序必須告訴他那個魔咒的功能;當哈利需要某個功能但不知道該用什麽魔咒時,你的程序要替他找到相應的魔咒。如果他要的魔咒不在詞典中,就輸出“what?”
Input首先列出詞典中不超過100000條不同的魔咒詞條,每條格式為:
[魔咒] 對應功能
其中“魔咒”和“對應功能”分別為長度不超過20和80的字符串,字符串中保證不包含字符“[”和“]”,且“]”和後面的字符串之間有且僅有一個空格。詞典最後一行以“@END@”結束,這一行不屬於詞典中的詞條。
詞典之後的一行包含正整數N(<=1000),隨後是N個測試用例。每個測試用例占一行,或者給出“[魔咒]”,或者給出“對應功能”。Output每個測試用例的輸出占一行,輸出魔咒對應的功能,或者功能對應的魔咒。如果魔咒不在詞典中,就輸出“what?”Sample Input
Sample Output
給你一部魔咒詞典。當哈利聽到一個魔咒時,你的程序必須告訴他那個魔咒的功能;當哈利需要某個功能但不知道該用什麽魔咒時,你的程序要替他找到相應的魔咒。如果他要的魔咒不在詞典中,就輸出“what?”
Input首先列出詞典中不超過100000條不同的魔咒詞條,每條格式為:
[魔咒] 對應功能
其中“魔咒”和“對應功能”分別為長度不超過20和80的字符串,字符串中保證不包含字符“[”和“]”,且“]”和後面的字符串之間有且僅有一個空格。詞典最後一行以“@END@”結束,這一行不屬於詞典中的詞條。
詞典之後的一行包含正整數N(<=1000),隨後是N個測試用例。每個測試用例占一行,或者給出“[魔咒]”,或者給出“對應功能”。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 @END@ 4 [lumos] the summoning charm [arha] take me to the sky
light the wand accio what? what?
題意 : 給你一個咒語對應著一個功能,要求有 q 此詢問,對於每次詢問給出相應的咒語後告訴你相應的功能
思路分析:這題用 map 可能會超內存,因此我們這裏用 hash 去做,對於 hash 後的值相同的情況下我們去連一條鏈,然後匹配的時候去在這條鏈上去匹配即可。
代碼示例:
using namespace std; #define ll unsigned long long const ll maxn = 1e6+5; char s[200]; struct node { char que[25]; char ans[85]; int next; }a[maxn], b[maxn]; ll p = 100007; char que_[25], ans_[85]; int ha[maxn], hb[maxn]; ll cura=0, curb=0; ll gethash(char *str){ ll res = 0; for(ll i = 0; *(str+i); i++){ res = res*p+(str[i]-‘a‘); } return res%p; } void insert(){ ll hash_a = gethash(que_); strcpy(a[cura].que, que_); strcpy(a[cura].ans, ans_); a[cura].next = ha[hash_a]; ha[hash_a] = cura; cura++; ll hash_b = gethash(ans_); strcpy(b[curb].que, que_); strcpy(b[curb].ans, ans_); b[curb].next = hb[hash_b]; hb[hash_b] = curb; curb++; //printf("++++ %llu %llu \n", hash_a, hash_b); } bool searcha(char *str){ ll num = gethash(str); int x= ha[num]; //printf("1111111111 %llu %d\n", num, x); while(x != -1){ //printf("_______ %s \n", a[x].que); if (strcmp(a[x].que, str) == 0){ printf("%s\n", a[x].ans); return true; } x = a[x].next; } return false; } bool searchb(char *str){ ll num = gethash(str); int x= hb[num]; //printf("2222222222 %llu %llu\n", num, x); while(x != -1){ if (strcmp(b[x].ans, str) == 0){ printf("%s\n", b[x].que); return true; } x = b[x].next; } return false; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); memset(ha, -1, sizeof(ha)); memset(hb, -1, sizeof(hb)); while(1){ gets(s+1); ll len = strlen(s+1); ll pos = 1; if (s[1] == ‘@‘) break; ll k = 0; for(ll i = 2; i <= len; i++){ if (s[i] == ‘]‘) {pos = i; break;} que_[k++] = s[i]; } que_[k] = ‘\0‘; k = 0; for(ll i = pos+2; i <= len; i++){ ans_[k++] = s[i]; } ans_[k] = ‘\0‘; insert(); } ll q; cin >>q; getchar(); while(q--){ gets(s); ll len = strlen(s); if (s[0] == ‘[‘){ s[len-1] = ‘\0‘; s[0] = ‘\0‘; if (!searcha(s+1)) printf("what?\n"); } else { if (!searchb(s)) printf("what?\n"); } } return 0; }
單詞匹配 - hash