三、搜索和二分 [Cloned] C - map

本文介绍了一种利用C++中的map数据结构实现语言翻译的方法。通过建立<string,string>类型的映射表,实现了从一种语言到另一种语言的快速查找与翻译。特别适用于初到异国他乡,面对语言障碍时的翻译需求。

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

原题:

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

题意:

到达一个新地方,不会当地的语言,但是有一本字典,输入当地语言,翻译成英语。

题解:

map的简单应用,创建一个<string,string>型的map,因为如果输入找不到对应的翻译就要输出一个eh,所以在创建一个<string,string>型的map来记录这个单词有没有出现过,别的就是简单的map输入问题了。

代码:AC

#include<iostream>
#include<string>
#include<map>
using namespace std;
 
int main()
{
	char english[11],foreign[11];
	map<string,bool>appear;
	map<string,string>translate;
	while(true)
	{
		char t;
		if((t=getchar())=='\n')
			break;
		else
		{
			english[0]=t;
			int i=1;
			while(true)
			{
				t=getchar();
				if(t==' ')
				{
					english[i]='\0';
					break;
				}
				else
					english[i++]=t;
			}
		}	
		cin>>foreign;
		getchar();
		appear[foreign]=true;
		translate[foreign]=english;
	}
	char word[11];
	while(cin>>word)
	{
		if(appear[word])
			cout<<translate[word]<<endl;
		else
			cout<<"eh"<<endl;
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值