SOJ 2037: Language of FatMouse

来源:互联网 发布:伦斯勒理工大学知乎 编辑:程序博客网 时间:2024/05/20 20:05


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 ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay
Sample Output
catehloops

 

 

分析:

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
原创粉丝点击