Babelfish

来源:互联网 发布:红牛为什么那么贵 知乎 编辑:程序博客网 时间:2024/06/05 03:11
B - Babelfish
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, 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. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops

Hint

Huge input and output,scanf and printf are recommended.

   好水的题,如果看不懂那就复习一下链表吧!

AC代码:

#include<iostream>#include<algorithm>#include<cstring>#include<string>#include<cstdio>using namespace std;struct S{string str;S* kid[26];S(){str="";for(int i=0;i<26;++i){kid[i]=NULL;}}}*root;void insert(string st,string str){S* t=root;int d;for(int i=0;str[i];++i){d=str[i]-'a';if(t->kid[d]==NULL){t->kid[d] = new S;}t=t->kid[d];if(!str[i+1])t->str+=st;}}string find(string str){S* t = root;int d;for(int i=0;str[i];++i){d = str[i]-'a';if(t->kid[d]==NULL){return "eh";}t = t->kid[d];}return t->str;}int main(){char s[100];string str,st;root = new S;while(gets(s)&&s[0]){int c=0,f=1;for(int i=0;s[i];++i){if(s[i]==' ')f=0,i++;if(f){str+=s[i];}elsest+=s[i];}insert(str,st);st="";str="";}while(cin >> str){cout << find(str) << endl;}return 0;}


超时代码:

#include<iostream>#include<algorithm>#include<string>#include<cstdio>#include<map>using namespace std;int main(){char s[100];string str,st;map<string,string> a;while(gets(s)&&s[0]){int c=0,f=1;for(int i=0;s[i];++i){if(s[i]==' ')f=0,i++;if(f){str+=s[i];}elsest+=s[i];}a[st]=str;st="";str="";}map<string,string>::iterator it;string ss;while(cin >> ss)  {  int f=1;  for(it=a.begin();it!=a.end();++it)  {  if(it->first==ss)  {  cout << it->second << endl;  f=0;break;  }  }  if(f) cout << "eh" << endl;  } a.clear();return 0;}


0 0
原创粉丝点击