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 ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
Huge input and output,scanf and printf are recommended.
这个题须要用到字典树,那么什么是字典树呢?
百度一下就可知道了------>地址是:Trie树
我比較喜欢用数组来写字典树。我在这放了两张图片!
希望能帮助理解!
本题题意:是要求我们将某一个星球的单词翻译成相应的英语,假设有就输出相应的英文,没有就不输出!
思路:这个题先得用某一个星球的单词来建立一个字典树,然后再将相应单词“放”在叶子节点后面,能够将其比喻为有坠子的耳环!然后在翻译时直接查找就可以!
然后就是处理最開始的一段输入的问题了!
gets(str)是以‘ ’作为结束符的,另外,它也是能够读入回车符的。所以就用gets(str)作为输入。用两个for循环便可轻松的将英语单词和某球单词分开。此处就不作其他优化了!然后就可进行建树了。
AC代码例如以下:
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; const int maxn=201314; struct Tree { int next[26]; char wei[26];//在相应的 树尾 上加一个尾巴! void init() { memset(next,-1,sizeof(next)); } } t[maxn]; int cur=1; void creat(char ss[],int len,char ci[]) { int p=0; for(int i=0; i<len; i++) { int x=ss[i]-'a'; if(t[p].next[x]==-1) { t[cur].init(); t[p].next[x]=cur++; } p=t[p].next[x]; } strcpy(t[p].wei,ci); } void cha(char ss[]) { int p=0,i=0; while(ss[i]) { int x=ss[i]-'a'; if(t[p].next[x]==-1) { cout<<"eh"<<endl; return ; } p=t[p].next[x]; i++; } cout<<t[p].wei<<endl; } int main() { t[0].init();//必须初始化! char str1[2013],str2[2014]; while(gets(str1)) { if(!strlen(str1)) break; char qian[2013],hou[2014]; int p=0; while(str1[p]!=' ') { qian[p]=str1[p]; p++;//把前面的单词提取出来!} qian[p]=' '; int cnt=0; for(int i=p+1; i<strlen(str1); i++) hou[cnt++]=str1[i];//提取后面的单词!
hou[cnt]=' '; creat(hou,strlen(hou),qian); } while(gets(str2)!=NULL) cha(str2); return 0; }