ZOJ1109-map与string

来源:互联网 发布:欧洲 旅游 大学生 知乎 编辑:程序博客网 时间:2024/05/17 06:28
Language of FatMouse

Time Limit: 10 Seconds      Memory Limit: 32768 KB

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

Input Specification

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 Specification

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

Output for Sample Input

catehloops


熟练掌握:
striing:  (声明:string s, char  line[size])
1.数组赋值给它的方法 s.assign(line);
2.在串中寻找字符的方法s.find(char);
3.得到子串的方法:s.substr(子字符串头,子字符串尾);也可以s.substr(子字符串头) 然后一直到串尾部。
map:
键值与映射map<string,string> m;
寻找键值 返回映射的方法!
it=m.find(temp)  *it.second;

#include<cstdio>#include<iostream>#include<map>#include<string>using namespace std;map<string,string>m;int main(){string a,b,s;char line[100];while(true){int pos;gets(line); //因为cin不能吸收空行, 所以只能用gets();s.assign(line);  //记住这个赋值方法if(s.empty()) break;pos=s.find(' ');a=s.substr(0,pos);     //注意这个得到字串的方法。b=s.substr(pos+1);m[b]=a;}string temp;map<string,string>::iterator it;while(cin>>temp){if(m.find(temp)!=m.end()){it=m.find(temp);cout<<(*it).second<<endl;}else cout<<"eh"<<endl;}return 0;}