• 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;
    }
  • 相关阅读:
    [bzoj1934][Shoi2007]Vote 善意的投票
    [bzoj1834][ZJOI2010]network 网络扩容
    [bzoj2127]happiness
    [bzoj3876][Ahoi2014]支线剧情
    [bzoj1927][Sdoi2010]星际竞速
    [bzoj3223]Tyvj 1729 文艺平衡树
    [bzoj3224]Tyvj 1728 普通平衡树
    FJOI2017 RP++
    [bzoj3529][Sdoi2014]数表
    异步ajax请求数据处理
  • 原文地址:https://www.cnblogs.com/jackge/p/2831028.html
Copyright © 2020-2023  润新知