Babelfish
Babelfish
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 20 Accepted Submission(s) : 7
Problem 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
Source
PKU
题目大意:
刚开始输入每行输入对应的映射字符串,和字符串。
然后,下面每行输入一串字符串,让你输出对应的映射字符串、
解法一:字典树(2016.4.18)
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #define MaxN 1008600 6 using namespace std; 7 const int Len=26; 8 struct DirTree{ 9 int Next[Len]; 10 int Num; 11 char SSS[20]; 12 }ID[500050]; 13 int Head;//头节点 14 int Pos;//节点编号 15 int Cread(int x){//创建新的节点 16 for(int i=0;i<Len;i++){ 17 ID[Pos].Next[i]=0;//表示节点不存在 18 }ID[Pos++].Num=x; 19 return Pos-1; 20 } 21 void Insert(char Str[],char Str0[],int h){//插入 22 for(int i=0;Str[i];i++){ 23 int Sign=Str[i]-'a'; 24 if(ID[h].Next[Sign]==0){ 25 ID[h].Next[Sign]=Cread(0); 26 } 27 h=ID[h].Next[Sign]; 28 ID[h].Num++; 29 } 30 for(int i=0;Str0[i];i++)ID[h].SSS[i]=Str0[i]; 31 ID[h].SSS[strlen(Str0)]='\0'; 32 } 33 int Find_(char Str[],int h){//查找 34 for(int i=0;Str[i];i++){ 35 int Sign=Str[i]-'a'; 36 if(ID[h].Next[Sign]!=0){ 37 h=ID[h].Next[Sign]; 38 }else { 39 printf("eh\n"); 40 return 0; 41 } 42 } 43 printf("%s\n",ID[h].SSS); 44 return ID[h].Num; 45 } 46 int main() 47 { 48 char Str[15],Str0[15]; 49 Head=Pos=0;Cread(0); 50 while(scanf("%s",Str)!=EOF){ 51 if(getchar()==32){ 52 scanf("%s",Str0); 53 Insert(Str0,Str,Head); 54 } 55 else Find_(Str,Head); 56 } 57 return 0; 58 }
解法二:用Map进行映射处理:(2016.4.10)
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <algorithm> 5 #include <map> 6 #include <string> 7 using namespace std; 8 map<string,string>MAP; 9 char Str1[1001011]; 10 char Str2[1001011]; 11 int main() 12 { 13 int Count=0; 14 while(scanf("%s",Str1)!=EOF){ 15 if(getchar()==32)scanf("%s",Str2); 16 else { 17 if(MAP[Str1].length()!=0)cout<<MAP[Str1]<<endl; 18 else cout<<"eh"<<endl; 19 continue; 20 } 21 MAP[Str2]=Str1; 22 } 23 return 0; 24 }
转载请备注:
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************
**************************************
* 作者: Wurq
* 博客: https://www.cnblogs.com/Wurq/
* Gitee: https://gitee.com/wurq
**************************************

浙公网安备 33010602011771号