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.
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;   
}  

 

目录
相关文章
F-POJ-3414 Pots
POJ-3414 Time Limit:1000 ms Memory Limit:65536 K Description You are given two po...
974 0
POJ 1804
题目:http://poj.org/problem?id=1804 大意:给你一串数字,排序。求出最少的交换次数  \ 我用归并做的 #include #include using namespace std; int aa[500010],bb[500010]; long lon...
674 0
poj-2551-ones
Description Given any integer 0
751 0
|
人工智能
POJ 1936 All in All
Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way.
760 0
|
存储
poj 1990 MooFest
点击打开poj 1990 思路: 树状数组 分析: 1 题目给定n头牛的听力v[i]. 现在规定两头你i和j如果要进行交流的话那么消耗的能量就是dis(i,j)*max(v[i].
730 0