POJ 2503

Babelfish
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 24748   Accepted: 10586

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.
/*
相对于下面的程序所做的优化为:建树时把 英文单词直接插在最后,
查找查到最后的结束标志时直接输出 ,省去了查找信息结构体的时间。
大致题意:输出火星文对应英文单词,若查不到,输出"eh"  
*/ 
#include <stdio.h>   
#include <string.h> 
#include <malloc.h>
#include <stdlib.h>  
const int N = 26;
int len;
typedef struct INFO
{
    char str1[12];
    char str2[12];
}INFO;
INFO ch[100010];
typedef struct Node    
{   
    int cnt;   
    Node *child[26];  
    char str_temp[15];  
}Node; 
Node *root;    
void init()
{//对根节点初始化,根节点不存储任何数据
    root =(Node *)malloc(sizeof(Node)); 
    for(int i = 0; i < N; i++)
        root->child[i] = NULL;
    root->cnt = 0; 
} 
void insert(char *str,char *str1)
{
    Node *current, *newnode;   
    int i, j,index;
    int len = strlen(str);    
    if(len == 0)
        return;//无须插入的情况
    current = root;
    for(i = 0; i < len; i++)
    {
        index = str[i]-'a';
        if(current->child[index]!=NULL)
        {//子树存在
            current = current->child[index];
            current->cnt++; 
        } 
        else
        {//子树不存在的情况 
            newnode =(Node *)malloc(sizeof(Node)); 
            for(j = 0; j < N; j++)
                newnode->child[j] = NULL;
            newnode->cnt = 1;            
            current->child[index] = newnode;//这一句不可少 
            current = newnode; //current = current->child[index]
        }
    }
    strcpy(current->str_temp,str1);//current是指针,所以要->不是.号 
}
void search( char *str )   
{ 
    int i, j, k, index;  
    Node *current, *newnode;    
    current = root;   
    for(i=0;str[i]!= '\0';++i )   
    {   
        index=str[i]-'a';   
        if(current->child[index]==NULL) 
        {   
            puts("eh");  
            return;
        }   
        current=current->child[index];   
    }
    if(str[i]=='\0')
    {
        puts(current->str_temp);
        return ;
    }        
}                  
int main()   
{   
    char str[30]; 
    int i=0,j,pos;
    init();  
    /*
    while(gets(str),strcmp(str, ""))   
        insert( str );  
        */
    memset(ch,0,sizeof(ch));
    while(1)
    {
        memset(str,0,sizeof(str));
        //scanf("%s %s",ch[i].str1,ch[i].str2);,scanf根本无法读入空行,所以无法比较 
        gets(str);
        if(strcmp(str,"")==0)
            break;
        len=strlen(str);
        pos=strchr(str,' ')-str+1;
        memcpy(ch[i].str1,str,pos-1);
        ch[i].str1[pos-1]='\0';
        memcpy(ch[i].str2,str+pos,len-pos);
        ch[i].str2[len-pos]='\0';   
        insert(ch[i].str2,ch[i].str1);
        i++;
    } 
    len=i;  
    memset(str,0,sizeof(str));     
    while(gets(str)!=NULL)   
    { 
        search(str);
        memset(str,0,sizeof(str));
    }   
    return 0;   
}  




//TLE 
#include <stdio.h>   
#include <string.h> 
#include <malloc.h>
#include <stdlib.h>  
const int N = 26;
int len;
typedef struct INFO
{
    char str1[12];
    char str2[12];
}INFO;
INFO ch[100010];
typedef struct Node    
{   
    int cnt;   
    Node *child[26];    
}Node; 
Node *root;    
void init()
{//对根节点初始化,根节点不存储任何数据
    root =(Node *)malloc(sizeof(Node)); 
    for(int i = 0; i < N; i++)
        root->child[i] = NULL;
    root->cnt = 0; 
} 
void insert(char *str)
{
    Node *current, *newnode;   
    int i, j,index;
    int len = strlen(str);    
    if(len == 0)
        return;//无须插入的情况
    current = root;
    for(i = 0; i < len; i++)
    {
        index = str[i]-'a';
        if(current->child[index]!=NULL)
        {//子树存在
            current = current->child[index];
            current->cnt++; 
        } 
        else
        {//子树不存在的情况 
            newnode =(Node *)malloc(sizeof(Node)); 
            for(j = 0; j < N; j++)
                newnode->child[j] = NULL;
            newnode->cnt = 1;            
            current->child[index] = newnode;//这一句不可少 
            current = newnode; //current = current->child[index]
        }
    }
}
void search( char *str )   
{ 
    int i, j, k, index;  
    Node *current, *newnode;    
    current = root;   
    for(i=0;str[i]!= '\0';++i )   
    {   
        index=str[i]-'a';   
        if(current->child[index]==NULL) 
        {   
            puts("eh");  
            return;
        }   
        current=current->child[index];   
    }         
    for(j=0;j<len;j++)//len全局变量,为洋文组数 
        if(strcmp(str,ch[j].str2)==0) 
        {
            puts(ch[j].str1); 
            break;
        }
}                  
int main()   
{   
    char str[30]; 
    int i=0,j,pos;
    init();  
    /*
    while(gets(str),strcmp(str, ""))   
        insert( str );  
        */
    memset(ch,0,sizeof(ch));
    while(1)
    {
        memset(str,0,sizeof(str));
        //scanf("%s %s",ch[i].str1,ch[i].str2);
        gets(str);
        if(strcmp(str,"")==0)
            break;
        len=strlen(str);
        pos=strchr(str,' ')-str+1;
        memcpy(ch[i].str1,str,pos-1);
        ch[i].str1[pos-1]='\0';
        memcpy(ch[i].str2,str+pos,len-pos);
        ch[i].str2[len-pos]='\0';   
        insert(ch[i].str2);
        i++;
    } 
    len=i;  
    memset(str,0,sizeof(str));     
    while(gets(str)!=NULL)   
    { 
        search(str);
        memset(str,0,sizeof(str));
    }   
    return 0;   
}  

 

posted @ 2012-08-01 16:53  加拿大小哥哥  阅读(452)  评论(0编辑  收藏  举报