Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 34816 | Accepted: 14908 |
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.
Source
题意:就是对应翻译,如果没有找到翻译就输出“eh”。
分析:首选就想到了map去做,然后AC了。最近在做字典树的题目,于是用字典树也写了一遍。
题目链接:http://poj.org/problem?id=2503
map代码:
#include<map> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MAX = 27 ; const int MAXN = 100000 + 5 ; map<string,int>m; int num = 0 , len; char s[MAXN][MAX],str[MAXN]; int main(){ //freopen("liuchu.txt","r",stdin); while(gets(s[num])&&s[num][0]!='\0'){ len = strlen(s[num]); int first = 0, k = 0; for(int i=0;i<len;i++){ if(first) str[k++]=s[num][i]; else if(s[num][i]==' '){ first = 1; s[num][i]='\0'; } } m[str]=num++; memset(str,'\0',sizeof(str)); } while(scanf("%s",str)!=EOF){ if(m.count(str)) printf("%s\n",s[m[str]]); else printf("eh\n"); } return 0; }
trie代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int MAX = 27 ; const int MAXN = 100000 + 5 ; struct trie{ int point; trie *next[MAX]; }; int number = 0 ; char s[MAXN][MAX],str[MAX]; trie *root=new trie; void createTrie(char *s,int n){ trie *p=root,*q; int len=strlen(s),pos; for(int i=0;i<len;i++){ pos=s[i]-'a'; if(p->next[pos]==NULL){ q=new trie; for(int j=0;j<MAX;j++) q->next[j]=NULL; p->next[pos]=q; p=p->next[pos]; } else{ p=p->next[pos]; } } p->point=n; } int findTrie(char *s){ trie *p=root; int len=strlen(s),pos; for(int i=0;i<len;i++){ pos=s[i]-'a'; if(p->next[pos]==NULL) return -1; p=p->next[pos]; } return p->point; } void delTrie(trie *Root){ for(int i=0;i<MAX;i++){ if(Root->next[i]!=NULL) delTrie(Root->next[i]); } free(Root); } int main(){ //freopen("liuchu.txt","r",stdin); for(int i=0;i<MAX;i++) root->next[i]=NULL; while(gets(s[number])&&s[number][0]!='\0'){ int len=strlen(s[number]),k=0 ; bool judge=false; for(int i=0;i<len;i++){ if(judge) str[k++]=s[number][i]; if(s[number][i]==' '){ s[number][i]='\0'; judge=true; } } createTrie(str,number); number++; } while(scanf("%s",str)!=EOF){ int pose=findTrie(str); if(pose==-1) printf("eh\n"); else printf("%s\n",s[pose]); } delTrie(root); return 0; }