您的位置:首页 > 其它

Poj:2503 Babelfish

2016-07-06 20:54 585 查看
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<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=100009;
struct trie
{
int next[32];
char str[26];
void init()
{
memset(next,0,sizeof(next));
memset(str,0,sizeof(str));
}
}tree[maxn*2];
int index;
void insert(char *s,char *t)
{
int len=strlen(s);
int p=0;
for(int i=0;i<len;i++)
{
int x=s[i]-'a';
if(tree[p].next[x]==0)
tree[p].next[x]=++index;
p=tree[p].next[x];
}
memcpy(tree[p].str,t,strlen(t));
}
void find(char *s)
{
int len=strlen(s);
int p=0;
for(int i=0;i<len;i++)
{
int x=s[i]-'a';
if(tree[p].next[x]==0)
{printf("eh\n");
return;
}

p=tree[p].next[x];
}
printf("%s\n",tree[p].str);
}
int main()
{
char st[32];
while(gets(st)&&st[0])
{
char str[22],ste[22];
int len=strlen(st);
int i,j;
for(i=0;st[i]!=' ';i++)
str[i]=st[i];
str[i]='\0';
for(i=i+1,j=0;i<len;j++,i++)
ste[j]=st[i];
ste[j]='\0';
//printf("%s %s\n",str,ste);
insert(ste,str);

}
char ss[12];
while(gets(ss))
find(ss);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj