Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 27412 | Accepted: 11842 |
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
#include<iostream> #include<stdio.h> #include<string> #include<map> using namespace std; //string str1,str2; map<string,string>m; char str[105],c_str1[50],c_str2[50]; int main() { while(gets(str) && str[0]!='\0') { sscanf(str,"%s %s",c_str1,c_str2); // 强大的sscanf() 函数 m[c_str2]=c_str1; //string 和char 数组可以兼容 } while(~scanf("%s",str)) { map<string,string>::iterator ret=m.find(str); //使用map 中的find()函数,如 m["12"]="a", //那么 m.find("12") 为 "a"的指针位置 if(ret==m.end()) puts("eh"); else puts((*ret).second.c_str()); //以c字符串的形式输出 //else cout<<(*ret).second<<endl; 以string 输出 } //map中 有两个元素,分别是m.first 和m.second return 0; }
快排 加上 二分查找:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct Node { char a[50],b[50]; }dic[100005]; int q_cmp(const void *x,const void *y) { return strcmp(((Node*)x)->b,((Node*)y)->b); } int b_cmp(const void *x,const void *y) { return strcmp((char*)x,((Node*)y)->b); } int main() { int n=0; char str[50],s1[50],s2[50]; while(gets(str)) { if(str[0]=='\0') { qsort(dic,n,sizeof(Node),q_cmp); // qsort break; } else { sscanf(str,"%s %s",s1,s2); strcpy(dic[n].a,s1); strcpy(dic[n].b,s2); n++; } } while(~scanf("%s",str)) { Node *p; p=(Node*)bsearch(str,dic,n,sizeof(Node),b_cmp); //二分查找 if(p) puts(p->a); else puts("eh"); } return 0; }