POJ 2503 Babelfish 字典树应用

Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 28090 Accepted: 12125

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.

题目大意:

      现在你到了一个城市,那里的人讲的话你听不懂,但是你有一本方言字典,输入的上半部分为字典,右边是方言,左边是对应的英文。输入的下半部分是要你翻译的单词,要求你输出要求你翻译的方言对应的英文。

解题思路:

      考虑到输入的字典单词对达到100,000,而每个单词的长度不会超过10,所以我们可以优先考虑用字典树对我们所谓的字典进行存储。而每查找一个方言所对应的英文不会超过10次。

代码如下:

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;

int cnt = 1;
struct Trie    //字典树节点,开一个char数组用于存储我们对应的英文
{
    Trie *next[26];
    char english[14] ;
};

Trie *root;

void initTrie ( Trie *root )    //初始化操作
{
    for ( int i = 0 ; i < 26 ; i ++ )
        root->next[i] = NULL;
}

void createTrie ( Trie * root , char *str1 , char *str2 )  //建立字典树的过程
{
    int len = strlen ( str2 );
    Trie *p = root,*q;
    for ( int i = 0 ; i < len ; i ++ )
    {
        int id = str2[i] - 'a';
        if ( p->next[id] == NULL )
        {
            q = new Trie;
            initTrie ( q );
            if ( i == len - 1 )    //建立到单词的末尾的时候,我们在这个节点里面放进我们对应的正规英文
            {
                strcpy ( q->english , str1 );
            }
            else q->english[0] = '\0';
            p->next[id] = q;
            p = p->next[id];
        }
        else
        {
            p = p->next[id];
        }
    }
}

char * findTrie ( char *str , Trie *root )    //查找过程
{
    int len = strlen ( str );
    Trie *p = root;
    for ( int i = 0 ; i < len ; i ++ )
    {
        int id = str[i] - 'a';
        p = p->next[id];
        if ( p == NULL ) return NULL;
    }
    return p->english;
}

int main()
{
    char str1[12],str2[12],str[25];
    Trie *root = new Trie;
    initTrie( root );
    while ( gets(str) , str[0] != '\0' )   //输入其实是个麻烦的事= =
    {
        int i,j;
        for ( i = 0 ; str[i] != ' ' ; i ++ )
            str1[i] = str[i];
        str1[i] = '\0';
        int len = strlen ( str );
        for ( i = i + 1 , j = 0 ; i < len ;  i ++ , j ++ )
            str2[j] = str[i];
        str2[j] = '\0';
        createTrie( root , str1 , str2 );
    }
    memset ( str , 0 , sizeof ( str ) );
    while ( scanf ( "%s" , str ) != EOF ) 
    {
        char *print = findTrie ( str , root );
        if ( print ) printf ( "%s\n" , print );
        else printf ( "eh\n" );
    }
    return 0;
}


技巧总结:

      当对于输入的数据数量大,但是长度很小的时候,我们应该快速的反应出用字典树进行查找。因为 len = strlen(str)<= 10,所以每次查找的次数<=10。 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值