A - ZJM 与霍格沃兹(必做)
ZJM 为了准备霍格沃兹的期末考试,决心背魔咒词典,一举拿下咒语翻译题
题库格式:[魔咒] 对应功能
背完题库后,ZJM 开始刷题,现共有 N 道题,每道题给出一个字符串,可能是 [魔咒],也可能是对应功能
ZJM 需要识别这个题目给出的是 [魔咒] 还是对应功能,并写出转换的结果,如果在魔咒词典里找不到,输出 “what?”
输入
首先列出魔咒词典中不超过100000条不同的咒语,每条格式为:
[魔咒] 对应功能
其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。魔咒词典最后一行以“@END@”结束,这一行不属于词典中的词条。
词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。
输出
每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果在词典中查不到,就输出“what?”
样例输入
[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?
思路
综述
这道题相当于给出一堆键值对,然后在里面搜索;
与普通的不同的是,这里面的键值对都是字符串,这就导致了一个问题,就是字符串在匹配的时候特别慢。
于是乎,将特定的字符串映射为特定的一个数字叫做Hash值。
所以匹配字符串就成了匹配Hash值;
hash函数实现如下
int tohash(string s){
int num = 1;
ull ans=0;
for(int i=0;i<s.size();i++){
int number = s[i];
ans += (number * quick_pow(seed,num)) ;
num++;
}
return ans;
}
鉴于num可能很大这里使用到了快速幂
ull quick_pow(int seed,int x){
ull ans=1;
while(x){
if(x & 1) ans *= seed ;
seed *= seed;
x >>= 1;
}
return ans;
}
坑点
- 为了防止hash值过大导致溢出,应该边计算hash边取模;
- seed相当于计算hash的一个参数,不宜取的太小,并且要是质数。
- hash值做出来为了避免负数的问题,用unsigned long long 更加方便
总结
有地方待提升,字符串的处理,写出来的总是很长很长容易出错误。
()()()
代码
#include <iostream>
#include <map>
#include <cstring>
#include <vector>
#define ull long long
using namespace std;
const int maxn = 1e5+10;
const ull seed = 131;
char tempc;
vector<string> e,c;
map<ull,int> e2c;
map<ull,int> c2e;
void init(){
e.resize(0);
c.resize(0);
}
ull quick_pow(int seed,int x){
ull ans=1;
while(x){
if(x & 1) ans *= seed ;
seed *= seed;
x >>= 1;
}
return ans;
}
int tohash(string s){
int num = 1;
ull ans=0;
for(int i=0;i<s.size();i++){
int number = s[i];
ans += (number * quick_pow(seed,num)) ;
num++;
}
return ans;
}
void in(){
string temps1,temps2;
while(1){
scanf("%c",&tempc);
if(tempc=='['){
temps1.clear();
temps2.clear();
while(1){
scanf("%c",&tempc);
if(tempc==']')break;
temps1.push_back(tempc);
}
getchar();
while(1){
scanf("%c",&tempc);
if(tempc=='\n')break;
temps2.push_back(tempc);
}
int num = e.size();
e.push_back(temps1);
c.push_back(temps2);
ull hash1;
ull hash2;
hash1 = tohash(temps1);
hash2 = tohash(temps2);
e2c[hash1] = num;
c2e[hash2] = num;
}
if(tempc=='@'){
for(int i=0;i<5;i++)scanf("%c",&tempc);
return;
}
}
}
void work(){
//work
string temps;
int n;
scanf("%d",&n);
getchar();
while(n--){
scanf("%c",&tempc);
if(tempc=='['){
temps.clear();
while(1){
scanf("%c",&tempc);
if(tempc == ']'){
getchar();
break;
}
temps.push_back(tempc);
}
ull hash1 = tohash(temps);
if(e2c.find(hash1)==e2c.end()){
printf("what?\n");
}else{
printf("%s\n",c[e2c[hash1]].c_str());
}
}else{
temps.clear();
temps.push_back(tempc);
while(1){
scanf("%c",&tempc);
if(tempc == '\n')break;
temps.push_back(tempc);
}
ull hash1 = tohash(temps);
if(c2e.find(hash1)==c2e.end()){
printf("what?\n");
}else{
printf("%s\n",e[c2e[hash1]].c_str());
}
}
}
}
int main(){
init();
in();
work();
}