Babelfish

Babelfish
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 41654   Accepted: 17670

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.

Source

/*
将词根插到字典树中,然后加一个标记这个词根是哪个单词的标记
*/
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#define N 30
#define M 100005
using namespace std;
#define MAX 26
const int maxnode=4000*100+100;///预计字典树最大节点数目
const int sigma_size=26;///每个节点的最多儿子数

char op[N];
char str1[M][N],str2[M][N];

struct Trie
{
    ///这里ch用vector<26元素的数组> ch;实现的话,可以做到动态内存
    int ch[maxnode][sigma_size];///ch[i][j]==k表示第i个节点的第j个儿子是节点k
    int val[maxnode];///val[i]==x表示第i个节点的权值为x
    int sz;///字典树一共有sz个节点,从0到sz-1标号

    ///初始化
    void Clear()
    {
        sz=1;
        memset(ch[0],0,sizeof(ch[0]));///ch值为0表示没有儿子
    }

    ///返回字符c应该对应的儿子编号
    int idx(char c)
    {
        return c-'a';
    }

    ///在字典树中插入单词s,但是如果已经存在s单词会重复插入且覆盖权值
    ///所以执行Insert前需要判断一下是否已经存在s单词了
    void Insert(char *s,int id)
    {
        int u=0,n=strlen(s);
        for(int i=0;i<n;i++)
        {
            int id=idx(s[i]);
            if(ch[u][id]==0)///无该儿子
            {
                ch[u][id]=sz;
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz++]=0;
            }
            u=ch[u][id];
        }
        val[u]=id;
    }

    ///在字典树中查找单词s
    int Search(char *s)
    {
        int n=strlen(s),u=0;
        for(int i=0;i<n;i++)
        {
            int id=idx(s[i]);
            if(ch[u][id]==0)
                return -1;
            u=ch[u][id];
        }
        return val[u];
    }
};
Trie trie;///定义一个字典树
int main()
{
    //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
    trie.Clear();
    int len=0;
    while(gets(op))
    {
        if(strcmp(op,"")==0)
            break;
        sscanf(op,"%s %s",str1[len],str2[len]);
        trie.Insert(str2[len],len);
        //cout<<str1[len]<<" "<<str2[len]<<endl;
        len++;
    }
    while(gets(op))
    {
        int cur=trie.Search(op);
        if(cur==-1)
            puts("eh");
        else
            printf("%s
",str1[cur]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6008409.html