百度空间 | 百度首页 
 
查看文章
 
pku2503 Babelfish
2009-04-26 23:29
Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9371 Accepted: 4083

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.

这道题有好多种解法,开始试着用map做了,不过这样就变的很水了,大概用了1200+ms。

后来改成用trie写了,自己随便写的,所以效率不高,不过还是500+ms就过了。比map快多了。

/*
ID :wangyucao
PROB:pku2503_Babelfish
LANG:C++
*/
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
using namespace std;
//FILE *f=fopen("c.2.dat","r");
//FILE *fp=fopen("c.txt","w");
struct Node
{
Node* ch[26];
char s[12];
Node()
{
   for(int i=0;i<26;i++)
   {
    ch[i]=NULL;
    strcpy(s,"");
   }
}
~Node()
{
   int i;
   for(i=0;i<26;i++)
    if(this->ch[i]!=NULL)
     delete this->ch[i];
}
};
Node *T;
char s[12],st[12];
void InsertT(Node* Tr,int i)
{
int j;
if(i==strlen(s))
{
   strcpy(Tr->s,st);
   return;
}
if(Tr->ch[s[i]-'a']!=NULL)
   InsertT(Tr->ch[s[i]-'a'],i+1);
else
{
   Node *p;
   p=new Node;
   Tr->ch[s[i]-'a']=p;
   InsertT(Tr->ch[s[i]-'a'],i+1);
}
}
void FoundT(Node *Tr,int i)
{
if(i==strlen(s))
{
   strcpy(st,Tr->s);
   return;
}
if(Tr->ch[s[i]-'a']!=NULL)
{
   FoundT(Tr->ch[s[i]-'a'],i+1);
}
}
int main()
{
int i,j;
char c;
T=new Node;
scanf("%c",&c);
while(c!='\n')
{
   scanf("%s%s",st+1,s);
        st[0]=c;
   scanf("%c",&c);  
   InsertT(T,0);
   scanf("%c",&c);
}
while(scanf("%s",s)!=EOF)
{
   st[0]='\0';
   FoundT(T,0);
   if(strlen(st)==0)
    strcpy(st,"eh");
   printf("%s\n",st);
}
return 0;
}


类别:acm/icpc | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu