二、stl ,模拟,贪心等 [Cloned] B - stl 的 map

原题:

Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?

题意:

输入一段外星语让我们翻译成英语,当然会给出一个对应的字典来让你查询,如果查询不到就按照原来的字母输出。用一个字符串对应字符串的map就可以大致解决问题。需要注意的几个问题是输入的格式(wa了几次),因为会有一些标点符号穿插在里边,所以不能直接直接输入然后查找一个个单词,只能一个个字母的查询,如果能组成单词才在map里找有没有对应的。

代码:AC

#include<iostream>
#include<cstring>
#include<map>
using namespace std;
int main()
{
	string a,b;
	map<string ,string>m;
	cin>>a;
	while(cin>>a&&a!="END")
	{
		cin>>b;
		m[b]=a;
	}
	cin>>a;
	char tem[3001];
	getchar();
	while(1)
	{
		gets(tem);
		if(strcmp(tem,"END")==0)
			break;
		int i,len;
		len=strlen(tem);
		b="";
		for(i=0;i<len;i++)
		{
			if(tem[i]<'a'||tem[i]>'z')
			{
				if(m[b]!="")
				cout<<m[b];
				else
				cout<<b;
				b="";
				cout<<tem[i];
			}
			else
			b+=tem[i];
		}
		cout<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/npuyan/article/details/81368980
今日推荐