map的使用

Description

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? 
 

Input

The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters. 
 

Output

In this problem, you have to output the translation of the history book. 
 

Sample Input

    
    
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 

Sample Output

    
    
hello, i'm from mars. i like earth!

TLE代码:

#include<iostream>
#include<map>
#include<string>
#include<algorithm>

using namespace std;

map<string, string>m;			//前面为古文,后面为英语
void deal(string & s)
{
	string s1;
	map<string, string>::iterator it = m.begin();
	for (; it != m.end(); ++it)
	{
		int pos;
		if ((pos=s.find((*it).first)) != -1)
		{
			s.replace(pos, (*it).first.size(), (*it).second);
		}
	}
	cout << s << endl;
}

int main()
{
	string b, s2, s1;
	cin >> b;
	while (cin >> s2 >> s1&&s1 != "START")
	{
		m.insert(make_pair(s1, s2));
	}
	int i;
	while (1)
	{
		string text;
		getline(cin, text);
		if (text[0] == 'E'&&text[1] == 'N'&&text[2] == 'D')
			break;
		deal(text);
	}
}

此段代码错误的原因便在于,没有仔细理解 all the punctuation should not be translated,虽然并不影响结果,但是很多标点影响执行效率,

你要把字典中的每个单词与之做对比,无疑效率大打折扣,应当把标点作为分隔符。还有一点就是不要将map中的每个单词逐一在字符串里寻找,替换。寻找时也是逐个字符进行匹配,效率低。不如一遍遍历字符串,每个标点作为分界点,每加入一个字符便在map里面匹配,map是有大小顺序的,时间复杂度log n,效率高。

如下:

#include<iostream>
#include<map>
#include<string>
#include<algorithm>

using namespace std;
map<string,string>m;			//前面为古文,后面为英语

void deal(char s[])
{
	string s1="";
	for(int i=0;i<strlen(s);i++)
    {
        if(s[i]<'a'||s[i]>'z')
        {
            if(m.count(s1))
            {
                cout<<m[s1];
                cout<<s[i];
                s1="";
            }
            else
            {
                cout<<s1;
				cout<<s[i];
                s1="";
            }
        }
		else
			s1=s1+s[i];
    }
	if(s1!="")
    {
        if(m.count(s1))
        {
            cout<<m[s1];
        }
        else
            cout<<s1;
    }
	cout<<endl;
}

int main()
{
	string b,s2,s1;
	cin>>b;
	while(cin>>s2>>s1&&s1!="START")
	{
		m.insert(make_pair(s1,s2));
	}
	char text[3002];
	int i;
	getchar();
	while(1)
	{
		gets(text);
		if(text[0]=='E'&&text[1]=='N'&&text[2]=='D')
			break;
		deal(text);
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值