题目链接:【http://acm.hdu.edu.cn/showproblem.php?pid=1880】
中文题面,题意很简单;
题解: 把每个 魔咒 和 对应的功能分别Hash,然后分别映射到map<ULL,string>里面,(魔咒Hash值,对应的功能)和(对应功能Hash值,魔咒)。
Hash 方式采用最简单的那种Hash即可。
#include<bits/stdc++.h> using namespace std; const int maxn = 100 + 15; const int sed = 131; typedef unsigned long long ULL; unordered_map<ULL, string>mpa, mpb; char tmp[maxn], sa[maxn], sb[maxn]; ULL get_Hash(char s[], int len) { ULL ret = 0; for(int i = 0; i < len; i++) { ret = ret * sed + s[i]; } return ret; } int main () { while(true) { gets(tmp); int len = strlen(tmp); if(!strcmp("@[email protected]", tmp)) break; int cnt = 0, pos = 0; for(int i = 1; i < len; i++) { if(tmp[i] == ‘]‘) { pos = i + 2; break; } sa[cnt++] = tmp[i]; } sa[cnt] = 0; ULL ta = get_Hash(sa, cnt); cnt = 0; for(int i = pos; i < len; i++) sb[cnt++] = tmp[i]; sb[cnt] = 0; ULL tb = get_Hash(sb, cnt); mpa[ta] = (string)(sb); mpb[tb] = (string)(sa); } int N; scanf("%d", &N); gets(tmp); for(int i = 1; i <= N; i++) { gets(tmp); int len = strlen(tmp); if(tmp[0] == ‘[‘) { for(int i = 0; i < len; i++) tmp[i] = tmp[i + 1]; tmp[len - 2] = 0; ULL T = get_Hash(tmp, len - 2); if(mpa.count(T)) cout << mpa[T] << endl; else cout << "what?" << endl; continue; } ULL T = get_Hash(tmp, len); if(mpb.count(T)) cout << mpb[T] << endl; else cout << "what?" << endl; } return 0; }
时间: 2024-11-03 16:30:49