zoukankan      html  css  js  c++  java
  • 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;   
    }  
    
  • 相关阅读:
    团队作业2
    团队作业2
    软件工程-团队作业1
    软件工程第一次作业
    自我介绍
    软件工程第一次作业
    Python3中采用PyInstaller打包工程项目
    mol2分子库文件拆分成单个mol2文件
    化学信息包安装
    单词读音音频源网址
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2618625.html
Copyright © 2011-2022 走看看