题目描述:
ZJM 为了准备霍格沃兹的期末考试,决心背魔咒词典,一举拿下咒语翻译题
题库格式:[魔咒] 对应功能
背完题库后,ZJM 开始刷题,现共有 N 道题,每道题给出一个字符串,可能是 [魔咒],也可能是对应功能
ZJM 需要识别这个题目给出的是 [魔咒] 还是对应功能,并写出转换的结果,如果在魔咒词典里找不到,输出 “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
Sample Output
light the wand
accio
what?
what?
题目分析:
这是一个字符串问题,要用hash来解决,那么hash中我们取seed=7,这样就代入公式可以直接计算了,只要不是空(你要注意到一句话里面中间有空格,他们是不算的)就能计算hash,不过要注意hash值非常大,考虑到他是非负,所以我们用unsigned long long。
unsigned long long hash_(char*str,int l,int r)
{
unsigned long long ans=0,temp=seed;
for(int i=l;i<=r;i++)
{
if(str[i]!=' ')
{
ans=ans+str[i]*temp;
temp=temp*temp;
}
}
return ans;
}
然后就要存储字典了,首先这个输入就很不一般,要注意的是单词中间有个空格,直接cin根本输不进去,所以我们要用scanf一股脑的先输入进去,然后用getchar读取一行。
while(scanf("%[^\n]",temp)&&strcmp(temp,"@END@")!=0)
{
getchar();
读取了之后要分成两部分走,一部分是方括号里的,这样我们最左端就是1(起始为0,也就是方括号左边),最右边就往右走直到走到了由方括号。然后就可以复制到第一个字符串里面了,计算hash然后保存到map里面
while(r<len&&temp[r]!=']')
{
r++;
}
strncpy(str1[n],temp+1,r-l);
unsigned long long h=hash_(temp,l,r-1);
mp[h]=n;
读取完了方括号内容之后就往后读取内容了,和前面一个套路,最左端是原来方括号里面最右端+2(一个是右方括号,一个是中间隔开的空格)。最右端就是总长度了,然后中间的所有字符都复制过去,计算hash值然后赋值到map里。
l=r+2;
r=len;
strncpy(str2[n],temp+l,r-l);
h=hash_(temp,l,r-1);
mp[h]=n;
n++;
然后就是找字典了,读入总共的数量之后就可以输入字符串了,考虑到和上面一样的情况,所以还是一样的读入方式,不过这次只需要读入一个了。
scanf("%[^\n]",temp);
getchar();
首先从第一个字符判断是方括号里面的还是外面的,这样根据不同的大小进行hash值的计算,计算的时候要把方括号去掉。
if(temp[0]=='[')
{
h=hash_(temp,1,len-2);
}
else
{
h=hash_(temp,0,len-1);
}
然后就可以在map里面找了,如果找不到说明不存在。
if(mp.find(h)==mp.end())
{
cout<<"what?"<<endl;
}
如果找到了就要根据第一个字符判断转换到方括号里面的还是外面的,然后就可以进行转换了。
if(temp[0]=='[')
{
cout<<str2[mp[h]]<<endl;
}
else
{
cout<<str1[mp[h]]<<endl;
}
代码如下:
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
int seed=7;
char str1[100010][30];
char str2[100010][100];
map<unsigned long long,int> mp;
unsigned long long hash_(char*str,int l,int r)
{
unsigned long long ans=0,temp=seed;
for(int i=l;i<=r;i++)
{
if(str[i]!=' ')
{
ans=ans+str[i]*temp;
temp=temp*temp;
}
}
return ans;
}
int main()
{
int n=0;
char temp[105];
while(scanf("%[^\n]",temp)&&strcmp(temp,"@END@")!=0)
{
getchar();
int len=strlen(temp);
int l=1,r=1;
while(r<len&&temp[r]!=']')
{
r++;
}
strncpy(str1[n],temp+1,r-l);
unsigned long long h=hash_(temp,l,r-1);
mp[h]=n;
l=r+2;
r=len;
strncpy(str2[n],temp+l,r-l);
h=hash_(temp,l,r-1);
mp[h]=n;
n++;
}
int num;
cin>>num;
getchar();
for(int i=0;i<num;i++)
{
scanf("%[^\n]",temp);
getchar();
int len=strlen(temp);
unsigned long long h;
if(temp[0]=='[')
{
h=hash_(temp,1,len-2);
}
else
{
h=hash_(temp,0,len-1);
}
if(mp.find(h)==mp.end())
{
cout<<"what?"<<endl;
}
else
{
if(temp[0]=='[')
{
cout<<str2[mp[h]]<<endl;
}
else
{
cout<<str1[mp[h]]<<endl;
}
}
}
}