zoukankan      html  css  js  c++  java
  • PKU Babelfish

    Babelfish

    Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)

    Total Submission(s) : 11   Accepted Submission(s) : 8

    Problem 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
     
    Source
    PKU
     
    解法一:Trie树
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    struct node{
        char wrd[15];
        node *next[26];
        node(){
            for(int i=0;i<26;i++)
                next[i]=NULL;
        }
    };
    
    node *root;
    
    void Insert(char *str,char *wrd){
        node *loc=root,*tmp;
        for(int i=0;str[i]!='\0';i++){
            int id=str[i]-'a';
            if(loc->next[id]!=NULL){
                loc=loc->next[id];
            }else{
                tmp=new node;
                loc->next[id]=tmp;
                loc=loc->next[id];
            }
        }
        strcpy(loc->wrd,wrd);
    }
    
    int SearchTrie(char *str,char *wrd){
        node *loc=root;
        for(int i=0;str[i]!='\0';i++){
            int id=str[i]-'a';
            if(loc->next[id]!=NULL)
                loc=loc->next[id];
            else
                return 0;
        }
        strcpy(wrd,loc->wrd);
        return 1;
    }
    
    void release(node *root){
        if(root==NULL)
            return ;
        for(int i=0;i<26;i++)
            if(root->next[i]!=NULL)
                release(root->next[i]);
        delete root;
        root=NULL;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        root=new node;
        char wrd[15],str[15];
        char s[40];
        while(gets(s)){
            if(strlen(s)==0)
                break;
            sscanf(s,"%s%s",wrd,str);
            Insert(str,wrd);
        }
        while(gets(str)){
            int flag=SearchTrie(str,wrd);
            if(flag)
                printf("%s\n",wrd);
            else
                printf("eh\n");
        }
        release(root);
        return 0;
    }
     
    #include<string.h>
    #include<stdlib.h>
    #include<stdio.h>
    
    #define MAX 26
    
    struct Trie{
        char *wrd;
        struct Trie *next[MAX];
    };
    
    struct Trie *Root;
    
    void Init(){
        Root=(Trie *)malloc(sizeof(Trie));
        for(int i=0;i<MAX;i++)
            Root->next[i]=NULL;
        Root->wrd=NULL;
    }
    
    void CreateTrie(char *str,char *wrd){
        Trie *location=Root,*tmp;
        while(location!=NULL && *str!='\0'){
            int id=*str-'a';
            if(location->next[id]==NULL){
                tmp=(Trie *)malloc(sizeof(Trie));
                for(int i=0;i<MAX;i++)
                    tmp->next[i]=NULL;
                tmp->wrd=NULL;
                location->next[id]=tmp;
            }
            location=location->next[id];
            str++;
        }
        if(location->wrd==NULL){
            location->wrd=new char[strlen(wrd)+1];
            strcpy(location->wrd,wrd);
        }
    }
    
    int SearchTrie(char *str,char *wrd){
        Trie *location=Root;
        while(location!=NULL && *str!='\0'){
            int id=*str-'a';
            location=location->next[id];
            str++;
        }
        if(location!=NULL && location->wrd!=NULL){
            strcpy(wrd,location->wrd);
            return 1;
        }else
            return 0;
    }
    
    int main(){
        char s[15],t[15];
        char buff[50];
        Init();
        while(gets(buff) && strcmp(buff,"")!=0){
            sscanf(buff,"%s%s",s,t);
            CreateTrie(t,s);
        }
        while(scanf("%s",s)!=EOF){
            if(SearchTrie(s,t))
                printf("%s\n",t);
            else
                printf("eh\n");
        }
        return 0;
    }

    解法二:hash表(待定,,转,,没大懂)

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #define COLUMN 100000
    #define LEN 11
    
    //char dic[COLUMN][LEN];
    struct node{
        char s[LEN];
        char sHash[LEN];
        struct node* next;
    }hashNode[COLUMN];
    
    unsigned int ELFHash(char* str)
    {
        unsigned int hash = 0;
        unsigned int x = 0;
    
        while (*str){
            hash = (hash << 4) + (*str++);
            if ((x = hash & 0xF0000000L) != 0){
                hash ^= (x >> 24);
                hash &= ~x;
            }
        }
        return (hash & 0x7FFFFFFF);
    }
    
    void insert(char str[], char sHash[])
    {
        int h = ELFHash(sHash)%COLUMN;
        struct node* p = &hashNode[h];
        while (p->next != NULL){
            p = p->next;
        }
        strcpy(p->s, str);
        strcpy(p->sHash, sHash);
        struct node* tmp = (struct node*)malloc(sizeof(struct node));
        tmp->next = NULL;
        p->next = tmp;
    }
    
    char* search(char sHash[])
    {
        int h = ELFHash(sHash)%COLUMN;
        struct node* p = &hashNode[h];
        while (p->next != NULL){
            if (!strcmp(sHash, p->sHash))
                return p->s;
            p = p->next;
        }
        return NULL;
    }
    
    int main(void)
    {
        char s1[LEN], s2[LEN];
        char buff[50];
        memset(hashNode, 0, sizeof(hashNode));
        while (gets(buff) && strcmp(buff, "") != 0){
            sscanf(buff, "%s%s", s1, s2);
            insert(s1, s2);
        }
    
        while (scanf("%s", s1) != EOF){
            char* p = search(s1);
            if (p)
                printf("%s\n", p);
            else
                printf("eh\n");
        }
        return 0;
    }
  • 相关阅读:
    vue-cli+webpack打包,上线
    vue2.0搭建vue手脚架(vue-cli)
    vue -- vue-cli webpack项目打包后自动压缩成zip文件
    Java字符串和常用类
    Java基础
    TF-IDF与TextRank的关键词提取算法应用
    ACl2019|使用Graph-to-Sequence模型为中文文章生成连贯的评论
    ACL2019|巧用文本语境信息:基于上下文感知的向量优化
    探索四川奥秘
    手机浏览器通过Scheme跳转APP,兼容各种手机浏览器
  • 原文地址:https://www.cnblogs.com/jackge/p/2831028.html
Copyright © 2011-2022 走看看