第十五周作业-必做1

本文介绍了一个基于魔法世界的字符串匹配问题,通过使用哈希算法实现魔咒词典的高效查询,解决魔咒与功能之间的转换。文章详细阐述了算法实现过程,包括哈希值计算、字典存储及查询流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:
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;
			}
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值