• POJ 2503 Babelfish


    Babelfish
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 27629   Accepted: 11948

    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.
    解题方法:简单的字典树。
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <string>
    #include <stdlib.h>
    #include <map>
    #include <algorithm>
    using namespace std;
    
    typedef struct node
    {
        node *next[26];
        char word[11];
        node()
        {
            for (int i = 0; i < 26; i++)
            {
                next[i] = NULL;
            }
            strcpy(word, "");
        }
    }TreeNode;
    
    void Insert(TreeNode *pHead, char word[], char foreign[])
    {
        TreeNode *p = pHead;
        int nLen = strlen(foreign);
        for (int i = 0; i < nLen; i++)
        {
            int index = foreign[i] - 'a';
            if (p->next[index] == NULL)
            {
                p->next[index] = new TreeNode;
            }
            p = p->next[index];
        }
        strcpy(p->word, word);
    }
    
    void Search(TreeNode *pHead, char foreign[])
    {
        TreeNode *p = pHead;
        int nLen = strlen(foreign);
        for (int i = 0; i < nLen; i++)
        {
            int index = foreign[i] - 'a';
            if (p->next[index] == NULL)
            {
                printf("eh
    ");
                return;
            }
            p = p->next[index];
        }
        if (strcmp(p->word, "") == 0)
        {
            printf("eh
    ");
        }
        else
        {
            printf("%s
    ", p->word);
        }
    }
    
    void DeleteNode(TreeNode *pHead)
    {
        if (pHead != NULL)
        {
            for (int i = 0; i < 26; i++)
            {
                DeleteNode(pHead->next[i]);
            }
        }
        delete pHead;
    }
    
    int main()
    {
        char temp[25];
        char str1[11], str2[11];
        TreeNode *pHead = new TreeNode;
        gets(temp);
        while(strcmp(temp, "") != 0)
        {
            int i = 0, j = 0;
            while(str1[i] = temp[j])
            {
                if (temp[j] == ' ')
                {
                    break;
                }
                i++;
                j++;
            }
            str1[i] = '';
            i = 0;
            j++;
            while(str2[i] = temp[j])
            {
                if (temp[j] == '')
                {
                    break;
                }
                i++;
                j++;
            }
            str2[i] = '';
            i = j = 0;
            Insert(pHead, str1, str2);
            gets(temp);
        }
        while(cin>>str2)
        {
            Search(pHead, str2);
        }
        DeleteNode(pHead);
        return 0;
    }
  • 相关阅读:
    How to convert VirtualBox vdi to KVM qcow2
    (OK)(OK) adb -s emulator-5554 shell
    (OK)(OK) using adb with a NAT'ed VM
    (OK) How to access a NAT guest from host with VirtualBox
    (OK) Creating manually one VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK)(OK) Creating VMs from an existing VDI file in CLI (VBoxManage) in Fedora 23
    (OK) Creating_VMs_from_an_existing_VDI_file.txt
    (OK) Creating VMs from an existing VDI file —— in OS X
    (OK) install_IBM_SERVER.txt
    (OK) install chrome & busybox in android-x86_64 —— uninstall chrome
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3178631.html
Copyright © 2020-2023  润新知