POJ-2503-Babelfish
Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 32156 Accepted: 13820
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
Waterloo local 2001.09.22
題意:
給你翻譯和原詞,讓你輸入原詞能輸出相應的翻譯,如果沒有對應的翻譯,輸出“eh”
思路:map會超時,Hash還沒有深入研究,用字典(trie)樹做過了
AC代碼:
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #define INF 0x11111110 using namespace std; struct node { node *next[26];//node類的26個子類 char str[15]; node(){//構造函數,用來初始化數據 memset(next,0,sizeof(next)); memset(str,0,sizeof(str)); } }; node *root=new node();//聲明一個初始node類 (這個類決定了以下所有子類,是字典樹的根) void insert(char *s,char *t)//插入新的字符串 { node *p=root; int i,k; for(i=0;s[i];i++){ k=s[i]-'a'; if(p->next[k]==NULL) p->next[k]=new node();//不存在此節點則創建 p=p->next[k];//移向下一個節點 } strcpy(p->str,t); } char *Find(char *s) { node *p=root; int i,k,n; n=strlen(s); for(i=0;i<n;i++){ k=s[i]-'a'; if(p->next[k]!=NULL) p=p->next[k];//移向下一個節點 else return "eh"; } return p->str; } char str[40],str1[20],str2[20]; int main() { int i,j,n,m,x; while(gets(str))//注意輸入,比較坑 { if(strcmp(str,"")==0) { memset(str,0,sizeof(str)); break; } n=strlen(str); for(i=0;i<n&&str[i]!=' ';i++) { str1[i]=str[i]; } i++;x=0; for(;i<n;i++) { str2[x++]=str[i]; } insert(str2,str1); memset(str1,0,sizeof(str1)); memset(str2,0,sizeof(str2)); memset(str,0,sizeof(str)); } while(scanf("%s",str)!=EOF) { printf("%s\n",Find(str)); memset(str,0,sizeof(str)); } return 0; }
最後更新:2017-04-03 05:39:41