Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 36951 | Accepted: 15743 |
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
acCODE:#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
struct Node{
char word[20];
char map[20];
}me[200000];
int cmp(const void *a,const void *b){
return strcmp(((Node*)a)->map,((Node*)b)->map);
};
int bcmp(const void *a,const void *b){
return strcmp(((char*)a),((Node*)b)->map);
};
int main(){
char s[60];
int n=0;
Node *pos;
while(gets(s)&&s[0]!='\0'){
sscanf(s,"%s %s",me[n].word,me[n].map);
n++;
}
qsort(me,n,sizeof(me[0]),cmp);
while(gets(s)&&s[0]!='\0'){
pos=(Node*)bsearch(s,me,n,sizeof(me[0]),bcmp);
if(pos)printf("%s\n",pos->word);
else printf("eh\n");
}
return 0;
}
暑假做的时候用二分现在用字典树在做一遍练练数据结构
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI acos(-1.0)
#define E exp(1)
#define MAX 27
using namespace std;
struct Trie{
Trie *next[MAX];
char tt[22];
int v;
};
bool flag;
Trie *root;
void createTrie(char *str,char *t){
int len=strlen(str);
Trie *p=root,*q;
FOR(i,0,len-1){
int id=str[i]-'a';
if(p->next[id]==NULL){
q=(Trie *)malloc(sizeof(Trie));
q->v=1;
FOR(j,0,MAX-1)
q->next[j]=NULL;
p->next[id]=q;
p=p->next[id];
}
else
p=p->next[id];
}
p->v=10;
strcpy(p->tt,t);
}
int find_Trie(char *str){
int len=strlen(str);
Trie *p=root;
FOR(i,0,len-1){
int id=str[i]-'a';
p=p->next[id];
if(p==NULL)
return 0;
}
if(p->v==10)
printf("%s\n",p->tt);
return 1;
}
char str[33],s1[16],s2[16];
int main(){
root=(Trie *)malloc(sizeof(Trie));
FOR(i,0,MAX-1)
root->next[i]=NULL;
while(gets(str)&&str[0]!='\0'){
sscanf(str,"%s %s",s2,s1);
createTrie(s1,s2);
}
while(rds(str)!=EOF){
flag=find_Trie(str);
printf(!flag?"eh\n":"");
}
return 0;
}
/*
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
*/