SOJ 2037: Language of FatMouse


We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation has already joined the WTO. Thanks to Turing we have computers to help him.

Input

Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. FatMouse words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
Sample Output
cat
eh
loops

 

 

分析:

1.输入:对于该输入遇到空行结束的问题 可以考虑使用getline 再配合stringstream按空格分割字符串

stringstream ss;
string line,t1,t2;
while (true)
{
	getline(cin,line);
	ss.str(line);
	ss>>t1>>t2;
	ss.clear();
	
}

之后使用map解决 但是发现该题输入量较大使用cin cout会超时

那么只能采用gets和puts的方法 结合sscanf如下:

 

	char temp[22],s1[22],s2[22];
	while(strcmp(gets(temp),"\0"))
	{
		sscanf(temp,"%s%s",s1,s2);
	}


 

因为gets puts方法没有办法结合string使用 只有开char数组 但char数组又没有办法结合map使用

此时则可以利用string的强大兼容性 与char数组进行转换  输出的时候用printf string的c风格串即可

代码如下:

#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdio>
#include <map>

using namespace std;


map<string,string>D;
int main()
{
//	ios::sync_with_stdio(false);
	int i;
	char temp[22],s1[22],s2[22];
	string t1,t2;
	while(strcmp(gets(temp),"\0"))
	{
		sscanf(temp,"%s%s",s1,s2);
		t1=s1;
		t2=s2;
		D[s2]=s1;
	}
	while(gets(temp)!=NULL)
	{
		t1=temp;
		if (D.find(t1)==D.end())
		{
			puts("eh");
		}
		else
		{
			
			printf("%s\n",D[t1].c_str());	
		}	
	}

	return 0;
}



stringstream ss;
string line,t1,t2;
while (true)
{
	getline(cin,line);
	ss.str(line);
	ss>>t1>>t2;
	ss.clear();
	
}


 

 总结:

1.输入遇到一行有多个单词(以空格相隔)可以使用puts+sscanf+char[]或者getline+stringstream+string

2.c风格的输出和输入无法兼容string时 可以先对char[]进行输入 然后赋值给string 输出的时候调用string的子函数转化为c风格串 这样就可以减少c++风格输入输出占用过多时间的矛盾


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值